Geeks With Blogs
Fringe SharePoint Continued

Part of working with Web Services is pushing data to SharePoint. There are numerous Blogs and articles that show you how to update, delete and add new list items. One of the things you will want to do is add metadata, specifically adding values to "User/Group" columns. The problem is that SharePoint has a specific format that needs to be followed.

If you look closely, underneath the covers SharePoint keeps an 'ID;#username' format. You might be wondering, "What is the ID for? and where does it come from?". Well, The short of it is that SharePoint assigns an ID to users that have been given access to a SharePoint Site. Tobias Zimmergren writes a great blog about it.

What you have to do is access the "People.aspx" and get the ID value. Here is a handy Class to help you with that: (the context in which I used this was in VSTA in an Infopath Form)

public class People

         WebServiceConnection _con;
        XPathNavigator _response;

        public People(WebServiceConnection con) {
            _con = con;

        public void SetPerson(string user) {
            XmlDocument doc = new XmlDocument();
            string ns = "";
            XmlElement SearchPrincipals = doc.CreateElement("SearchPrincipals",ns);
            XmlElement searchText = doc.CreateElement("searchText", ns);
            XmlElement maxResults = doc.CreateElement("maxResults", ns);
            XmlElement principalType = doc.CreateElement("principalType", ns);
            searchText.InnerText = user;
            maxResults.InnerText = "1";
            principalType.InnerText = "User SecurityGroup SharePointGroup DistributionList";


            _response = SearchPrincipals.CreateNavigator();       

        public XPathNavigator Execute() {
            XmlDocument doc = new XmlDocument();
            XPathNavigator output = doc.CreateNavigator();
            XmlDocument dd = new XmlDocument();
            XPathNavigator error = dd.CreateNavigator();
                _con.Execute(_response, output, error);
            catch (Exception e)

            return output;   

The calling code looks like this:

            People peopleasmx = new People((WebServiceConnection)DataConnections["People"]);
            XPathNavigator resp = peopleasmx.Execute();

The DataConnections variable allows me to access the data connections created through Infopath. I just called it "People". What you get back in the XPathNavigator is the following:

<SearchPrincipalsResponse xmlns="">
      <AccountName>ARWEN\Juan Larios</AccountName>
      <DisplayName>ARWEN\Juan Larios</DisplayName>

To get to any of the fields you can use an XPathExpression, use the one listed bellow to get the UserInfoID:(for GetXpathFromQuery method refer to my previous post)

            XPathExpression exp = resp.Compile("/x:SearchPrincipalsResponse/x:SearchPrincipalsResult/x:PrincipalInfo/x:UserInfoID");
            XPathNodeIterator iterator = GetXPathFromQuery(resp, exp);
            string number = iterator.Current.Value;

Once you have the ID all you do is concatenate the ID + ; + # + AccountName  and that's what you would add to SharePoint.

Posted on Monday, November 22, 2010 9:07 PM SharePoint , Development , SharePointUserGroup | Back to top

Comments on this post: Adding data to a 'User/Group' SharePoint Column

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © juanlarios | Powered by: