2

I've a requirement to create user in a SharePoint using the REST API and c#. How to achieve this?

Most of the article show using REST API in jquery and JavaScript but I've a requirement to do this using c#. And also I checked in MSDN for the same but they have given the JavaScript code where <host web url> is given where do I get the host web url. I'm using SharePoint 2013 which I'm accessing remotely. I don't have server side access so I need to do it through Rest API.

Please suggest some idea.

1

1 Answer 1

0
 protected void btnItemCreation_Click(object sender, EventArgs e)
 { 
    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
    {
       using (SPWeb web = site.OpenWeb())
       {
          SPListItem item;
          SPList list = web.Lists.TryGetList("List_Name");
          item = list.Items.Add();
          item["People_or_Group_Name"] = GetpeopleProjectManagerValues();
          item.Update();
       }
   }
}
protected SPFieldUserValueCollection GetpeopleProjectManagerValues()
        {
            try
            {
                string[] userarray = PeoplePickerID.CommaSeparatedAccounts.ToString().Split(',');
                SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
                for (int i = 0; i < userarray.Length; i++)
                {
                    SPFieldUserValue usertoadd = ConvertLoginAccount(userarray[i]);
                    usercollection.Add(usertoadd);
                }

                return usercollection;
            }
            catch (Exception ex)
            {

            }
        }
public SPFieldUserValue ConvertSaveLoginAccount(string userid)
        {
            SPFieldUserValue uservalue = null;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPUser requireduser = web.EnsureUser(userid);
                        uservalue = new SPFieldUserValue(web, requireduser.ID, requireduser.Name);
                    }
                }
            }
            catch (Exception ex)
            {

                }
            return uservalue;
        }
2
  • Try this above code to add any SharePoint user to Insert data using People Picker Commented Jan 27, 2016 at 7:38
  • 2
    The question was about using C# and the REST API. Your code gives an example of C# and server side code. Commented Jan 27, 2016 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.