A possible usage of People.asmx and UserGroup.asmx and Lists.asmx


Once I got a requirement to add values to the created by & modified by columns through a web service. I had already written a post on how we can update those columns using Lists.asmx web service and you can get it here.


So if you want to add new item then the only change you need to do here is add the method type as “New” instead of “Update”.



<Batch OnError=”Continue” ViewName=”{D0E978D3-4D39-4CBE-ACEC-BCFB22344252}”>


    <Method ID=’1′ Cmd=’New’>


        <Field Name=’Editor’>234;#Louis</Field>


     </Method>


</Batch>


 


Also you will get all the list of users already there in your site with the help of UserGroup.asmx web service. GetUserCollectionFromSite() will give you the list of all users in a particular site collection and once you got that information using Lists.asmx web service you can add or update them to People/Group type columns.


 


But consider the scenario that, you want to add some users but not added them to the site already. In that case it will be a little tricky situation, because we can add the user if he has a SharePoint user ID.


 


So what we need to do that, first add a reference to People.asmx web service. This web service has the following web methods.


 


People


 


The following operations are supported. For a formal definition, please review the Service Description.


·        ResolvePrincipals


·        SearchPrincipals


 


Now, we can use ResolvePrincipals() web method to add a new user to the UserInfo (People and Groups) list. This web method will return an array of PincipalInfo objects. Once you got that , then you can retrieve the user ID using UserInfoID property. Please check the below sample code snippet. “TPS\\boydt”  is “domainname\username”. 



   1: PrincipalInfo[] principleInfo = people.ResolvePrincipals(new string[] { “TPS\\boydt” }, SPPrincipalType.User, true);
   2: Console.WriteLine(principleInfo[0].UserInfoID);
   3: Console.ReadLine();

So once you got the UserID then you can add that user using Lists.asmx webservice.


 


Now we can come to the other scenario in which users are already there in the site collection. In this scenario you can straight away use UserGroup.asmx web service and once you got the users then you can add them to the site using Lists.asmx web service.


 


(click here to get to know how we can use lists.asmx web serivice to update the people/group column values. Remember if you are adding a new user then change the method type as “New”)


 


Code for getting the User information from a site collection using UserGroup.asmx.



   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4: using System.Xml;
   5: using Microsoft.SharePoint;
   6: using Microsoft.SharePoint.Administration;
   7: using ConsoleApplication2.localhost;
   8: using ConsoleApplication2.UserInfoWS;
   9:  
  10: namespace ConsoleApplication2
  11: {
  12:     class Program
  13:     {
  14:         static void Main(string[] args)
  15:         {
  16:             UserInfoWS.UserGroup oUserGroup = new UserGroup();
  17:             oUserGroup.UseDefaultCredentials = true;
  18:             oUserGroup.Url = “http://blr3r07-19c/sites/mycollabsite/_vti_bin/usergroup.asmx”;
  19:             
  20:             try
  21:             {
  22:                 XmlNode ndUsers = oUserGroup.GetUserCollectionFromSite();
  23:                 
  24:                 string intUserID = “”;
  25:                 string strLoginName = “”;
  26:  
  27:                 XmlNodeList oNodes = ndUsers.ChildNodes;
  28:  
  29:                 foreach (XmlNode node in oNodes)
  30:                 {
  31:                     XmlNodeReader objReader = new XmlNodeReader(node);
  32:  
  33:                     while (objReader.Read())
  34:                     {
  35:  
  36:                         if (objReader[“ID”] != null && objReader[“LoginName”] != null)
  37:                         {
  38:                             intUserID = objReader[“ID”].ToString();
  39:                             strLoginName = objReader[“LoginName”].ToString();
  40:                             Console.WriteLine(intUserID.ToString() + “#;” + strLoginName);                           
  41:                         }
  42:  
  43:                     }
  44:  
  45:                 }
  46:             }
  47:             catch (System.Web.Services.Protocols.SoapException ex)
  48:             {
  49:                 Console.WriteLine(“Message:\n” + ex.Message + “\nDetail:\n” + 
  50:                     ex.Detail.InnerText + 
  51:                      “\nStackTrace:\n” + ex.StackTrace);
  52:             }
  53:             Console.ReadLine();
  54:         }
  55:     }
  56: }
  57:  

Please check my other post regarding UserGroup.asmx for getting more familiar with some other web methods here


Thanks


Comments (3)

  1. kevinidzi says:

    An easier way to get at the lists.asmx is included in the Sharepoint to SSIS Adapter project: http://www.codeplex.com/SQLSrvIntegrationSrv/Release/ProjectReleases.aspx?ReleaseId=17652

    Included in there is a DLL used by the Adapter which wraps the Lists.asmx and simnplifies using it to get or update items.  It supports all of the batching and things you can do manually, but makes it as easy as populating a dictionary of key/values and telling it to update a give listname.

    – Kevin

  2. kevinidzi says:

    There is a SharePointUtility object, with a ListsServiceUtility that wraps getting/setting/deleting sharepoint list items and files (for a document list).  

    Here is a direct link to the base folder of the code, it’s public for viewing. Make sure before using, you are looking at the most recent version, this is just to point you in the right direction.

    http://www.codeplex.com/SQLSrvIntegrationSrv/SourceControl/DirectoryView.aspx?SourcePath=%24%2fSQLSrvIntegrationSrv%2fmain%2fSharePointListAdapters%2fSrc%2fSharePointUtility&changeSetId=17738

    – Kevin

Skip to main content