How to modify the custom People/Groups type column of a SharePoint List using Lists.ASMX


SharePoint has more than ten out-of-the-box webservices which will provide lots of functionalities that we want. Lists.asmx is one of such SharePoint webservice that help us to do certain operations with SharePoint lists. We can locate that webservice physically in the following location. 


C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\isapi


Now we can come to our requirment. If you want to update any column values in SharePoint lists we can consume Lists.asmx to accomplish that requirment. Below, I am giving a sample code snippet for updating the custom People/Groups type column values of a SharePoint list.


/******** Code snippet for modifying the custom People/Groups type column of a SharePoint List using Lists.ASMX*******/


/******** Written in ASP.NET Application **********/


 


protected void Button1_Click(object sender, EventArgs e)


    {


        /*Declare and initialize a variable for the Lists Web service.*/


        ListService.Lists oListService = new ListService.Lists();


 


        /*Authenticate the current user by passing their default


        credentials to the Web service from the system credential cache.*/


        oListService.Credentials =


            System.Net.CredentialCache.DefaultCredentials;


 


        /*Set the Url property of the service for the path to a subsite.*/


        oListService.Url =


             


        /*Create an XmlDocument object and construct a Batch element and its attributes. Note that an empty ViewName parameter causes the method to use the default view. */


        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();


        System.Xml.XmlElement batchElement = doc.CreateElement(“Batch”);


        batchElement.SetAttribute(“OnError”, “Continue”);


        batchElement.SetAttribute(“ListVersion”, “1”);       


        batchElement.SetAttribute(“ViewName”, “”);    


       


        /*Specify methods for the batch post using CAML. In each method


        include the ID of the item to update and the value to place in the


        specified column.*/


        batchElement.InnerXml = “<Method ID=’1′ Cmd=’Update’>” +


            “<Field Name=’ID’>9</Field>” +


            “<Field Name=’Title’ Type=’Text’>vvvv</Field>” +            


             “<Field Name=’Author’ Type=’User’ frombasetype=’FALSE’>14;#Sowmyan Soman</Field>” + // this column couldn’t update through webservice if you want to know how we can do that please refer here


              “<Field Name=’Users’ Type=’User’ frombasetype=’FALSE’>14;# Sowmyan Soman</Field>” +  


            “</Method>”;         


 


        XmlNode oNode = oListService.UpdateListItems(“Shared Documents”, batchElement);


        Response.Write(oNode.OuterXml);


    }


 


Comments (5)

  1. A possible usage of People.asmx and UserGroup.asmx and Listss.asmx Once I got a requirement to add the

  2. kevinidzi says:

    When updating any lookup field (user being one type), i’ve found you just need the ID, not the ;#<description> for updating / creating data.  The extra ;# info is nice for Gettign data, but ignored when doing updates.

  3. David says:

    I’ve got all this working.. however, I’m using an Asp.net form to update a Sharepoint list, one of the column is a People Column if I format the CAML by hand like "14;# Sowmyan Soman" it is fine… however I can’t work out where the UserId 14 comes from.. the userID that comes from ResolvePrincipals and GetUserInfo methods don’t match.  However the _layouts/userdisp.aspx?ID={UserId} does match the ID that should be in the CAML.

    I’m tearing my hair out trying to find this ID.

  4. sowmyancs says:

    Your findings are correct, please use people.asmx to work-around this. Please check my post :

    http://blogs.msdn.com/sowmyancs/archive/2008/09/10/a-possible-usage-of-people-asmx-and-usergroup-asmx-and-lists-asmx.aspx