HOW TO: Programmatically modify values of people and groups column of type multi-value using Lists.asmx in SharePoint 2010

Published 05-01-2019 03:37 PM 99 Views
Microsoft

First published on TECHNET on Mar 13, 2013

This blog post is a contribution from Bharat Rathod, an engineer with the SharePoint Developer Support team.

In this post, I’ll show you how to use UpdateListItems method of the Lists.asmx web service to modify a ListItem which has multi-value people and group column.  Most of us tend to fall into trouble while creating the batch element.

Here’s the detailed description of the sample code.

using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


using Microsoft.SharePoint;


using System.Xml;


using Microsoft.SharePoint.SoapServer;





namespace TestProject


{


class Program


{


static void Main(string[] args)


{


using (SPSite osite = new SPSite("http://sp/"))


{





using (SPWeb oweb = osite.OpenWeb())


{


SPList mylist = oweb.Lists["TestList"];


SPListItem item = mylist.GetItemById(1);





// Get the user ID of the User whose value needs to be added to the column


SPUser user1 = oweb.AllUsers["contoso\\user1"];


int Id1 = user1.ID;





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


list.UseDefaultCredentials = true;





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


XmlElement batchElement = doc.CreateElement("Batch");


batchElement.SetAttribute("OnError", "Continue");


batchElement.SetAttribute("ListVersion", "1");


batchElement.SetAttribute("ViewName", "");





batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +


"<Field Name='ID'>1</Field>" +


"<Field Name='Title'>Item1</Field>" +


"<Field Name='manyusers'>;#1;#Contoso\\spadmin;#9;#contoso\\user1;#</Field>" +


"</Method>";








try


{


list.UpdateListItems(mylist.ID.ToString(), batchElement);


}


catch (SoapServerException ex)


{


Console.WriteLine(ex.Message);


}





}


}








}


}


}



One of the most important part of this code is creating the batch element.  In order to add users to a multi-value people and group field, we need to get the UserID from the SPWeb object.  In the code, I’ve used the following statements:





SPUser user1 = oweb.AllUsers["contoso\\user1"];


int Id1 = user1.ID;



This code finds out the user’s identifier (UserID) of whatever user you want to add to the multi-value field (in this case, it’s contoso\user1 ).





batchElement.InnerXml = "<Method ID='1' Cmd='Update'>" +


"<Field Name='ID'>1</Field>" +


"<Field Name='Title'>Item1</Field>" +


"<Field Name='manyusers'>;#1;#Contoso\\spadmin;#9;#contoso\\user1;#</Field>" +


"</Method>";



The above code snippet tries to add two users to the column named “manyusers”.  Use the ;# symbols before every entity and then end the string with ;# again.  One common mistake we tend to do is to miss out on the ;# towards the end of the string.



Try the above sample to get users added to a multi-value people and group field.



Hope this short blog post is of some help!

%3CLINGO-SUB%20id%3D%22lingo-sub-509734%22%20slang%3D%22en-US%22%3EHOW%20TO%3A%20Programmatically%20modify%20values%20of%20people%20and%20groups%20column%20of%20type%20multi-value%20using%20Lists.asmx%20in%20SharePoint%202010%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-509734%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3E%20First%20published%20on%20TECHNET%20on%20Mar%2013%2C%202013%20%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThis%20blog%20post%20is%20a%20contribution%20from%20Bharat%20Rathod%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team.%3C%2FP%3E%0A%3CP%3EIn%20this%20post%2C%20I%E2%80%99ll%20show%20you%20how%20to%20use%20UpdateListItems%20method%20of%20the%20Lists.asmx%20web%20service%20to%20modify%20a%20ListItem%20which%20has%20multi-value%20people%20and%20group%20column.%26nbsp%3B%20Most%20of%20us%20tend%20to%20fall%20into%20trouble%20while%20creating%20the%20batch%20element.%3C%2FP%3E%0A%3CP%3EHere%E2%80%99s%20the%20detailed%20description%20of%20the%20sample%20code.%3C%2FP%3E%0A%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%0A%3CDIV%20id%3D%22codeSnippet%22%3Eusing%20System%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eusing%20System.Collections.Generic%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eusing%20System.Linq%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eusing%20System.Text%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eusing%20Microsoft.SharePoint%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eusing%20System.Xml%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eusing%20Microsoft.SharePoint.SoapServer%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Enamespace%20TestProject%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eclass%20Program%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Estatic%20void%20Main(string%5B%5D%20args)%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eusing%20(SPSite%20osite%20%3D%20new%20SPSite(%22http%3A%2F%2Fsp%2F%22))%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eusing%20(SPWeb%20oweb%20%3D%20osite.OpenWeb())%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESPList%20mylist%20%3D%20oweb.Lists%5B%22TestList%22%5D%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESPListItem%20item%20%3D%20mylist.GetItemById(1)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20Get%20the%20user%20ID%20of%20the%20User%20whose%20value%20needs%20to%20be%20added%20to%20the%20column%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESPUser%20user1%20%3D%20oweb.AllUsers%5B%22contoso%5C%5Cuser1%22%5D%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eint%20Id1%20%3D%20user1.ID%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EListService.Lists%20list%20%3D%20new%20ListService.Lists()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Elist.UseDefaultCredentials%20%3D%20true%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EXmlDocument%20doc%20%3D%20new%20System.Xml.XmlDocument()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EXmlElement%20batchElement%20%3D%20doc.CreateElement(%22Batch%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EbatchElement.SetAttribute(%22OnError%22%2C%20%22Continue%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EbatchElement.SetAttribute(%22ListVersion%22%2C%20%221%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EbatchElement.SetAttribute(%22ViewName%22%2C%20%22%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EbatchElement.InnerXml%20%3D%20%22%3CMETHOD%20id%3D%22'1'%22%20cmd%3D%22'Update'%22%3E%22%20%2B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%22%3CFIELD%20name%3D%22'ID'%22%3E1%3C%2FFIELD%3E%22%20%2B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%22%3CFIELD%20name%3D%22'Title'%22%3EItem1%3C%2FFIELD%3E%22%20%2B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%22%3CFIELD%20name%3D%22'manyusers'%22%3E%3B%231%3B%23Contoso%5C%5Cspadmin%3B%239%3B%23contoso%5C%5Cuser1%3B%23%3C%2FFIELD%3E%22%20%2B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%22%3C%2FMETHOD%3E%22%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Etry%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Elist.UpdateListItems(mylist.ID.ToString()%2C%20batchElement)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Ecatch%20(SoapServerException%20ex)%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EConsole.WriteLine(ex.Message)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EOne%20of%20the%20most%20important%20part%20of%20this%20code%20is%20creating%20the%20batch%20element.%26nbsp%3B%20In%20order%20to%20add%20users%20to%20a%20multi-value%20people%20and%20group%20field%2C%20we%20need%20to%20get%20the%20UserID%20from%20the%20SPWeb%20object.%26nbsp%3B%20In%20the%20code%2C%20I%E2%80%99ve%20used%20the%20following%20statements%3A%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3ESPUser%20user1%20%3D%20oweb.AllUsers%5B%22contoso%5C%5Cuser1%22%5D%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Eint%20Id1%20%3D%20user1.ID%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EThis%20code%20finds%20out%20the%20user%E2%80%99s%20identifier%20(UserID)%20of%20whatever%20user%20you%20want%20to%20add%20to%20the%20multi-value%20field%20(in%20this%20case%2C%20it%E2%80%99s%20%3CEM%3E%20contoso%5Cuser1%20%3C%2FEM%3E%20).%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22codeSnippetWrapper%22%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22codeSnippet%22%3E%3CBR%20%2F%3EbatchElement.InnerXml%20%3D%20%22%3CMETHOD%20id%3D%22'1'%22%20cmd%3D%22'Update'%22%3E%22%20%2B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%22%3CFIELD%20name%3D%22'ID'%22%3E1%3C%2FFIELD%3E%22%20%2B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%22%3CFIELD%20name%3D%22'Title'%22%3EItem1%3C%2FFIELD%3E%22%20%2B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%22%3CFIELD%20name%3D%22'manyusers'%22%3E%3B%231%3B%23Contoso%5C%5Cspadmin%3B%239%3B%23contoso%5C%5Cuser1%3B%23%3C%2FFIELD%3E%22%20%2B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%22%3C%2FMETHOD%3E%22%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EThe%20above%20code%20snippet%20tries%20to%20add%20two%20users%20to%20the%20column%20named%20%E2%80%9Cmanyusers%E2%80%9D.%26nbsp%3B%20Use%20the%20%3CSTRONG%3E%20%3B%23%20%3C%2FSTRONG%3E%20symbols%20before%20every%20entity%20and%20then%20end%20the%20string%20with%20%3CSTRONG%3E%20%3B%23%20%3C%2FSTRONG%3E%20again.%26nbsp%3B%20One%20common%20mistake%20we%20tend%20to%20do%20is%20to%20miss%20out%20on%20the%20%3CSTRONG%3E%20%3B%23%20%3C%2FSTRONG%3E%20towards%20the%20end%20of%20the%20string.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3ETry%20the%20above%20sample%20to%20get%20users%20added%20to%20a%20multi-value%20people%20and%20group%20field.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EHope%20this%20short%20blog%20post%20is%20of%20some%20help!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-509734%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20TECHNET%20on%20Mar%2013%2C%202013%20This%20blog%20post%20is%20a%20contribution%20from%20Bharat%20Rathod%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-509734%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Sep 01 2020 03:10 PM
Updated by: