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.

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;
        }
  • Try this above code to add any SharePoint user to Insert data using People Picker – Jayakumar Kulkarni Jan 27 '16 at 7:38
  • 1
    The question was about using C# and the REST API. Your code gives an example of C# and server side code. – jcp Jan 27 '16 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.