步步为营 SharePoint 开发学习笔记系列 十、SharePoint web service 开发(下)

来源:岁月联盟 编辑:exp 时间:2011-08-02

 

概要

 

接下来我们介绍Lists.UpdateListItems 在更新item做法和UserGroup.GetUserCollectionFromSite()的用法,请先学习步步为营SharePoint 开发学习笔记系列 八、SharePoint web service 开发(上),你将更容易学习web service的开发

 

Lists.UpdateListItems的用法

 

更新普通list的item的xml格式

 

view sourceprint?01 <Batch OnError="Continue" ListVersion="1" 

 

02 ViewName="270C0508-A54F-4387-8AD0-49686D685EB2"> 

 

03    <Method ID="1" Cmd="Update"> 

 

04       <Field Name="ID">4<Field> 

 

05       <Field Name="Field_Name">Value</Field> 

 

06    </Method> 

 

07    <Method ID="2" Cmd="Update"> 

 

08       <Field Name="ID" >6</Field> 

 

09       <Field Name="Field_Name">Value</Field> 

 

10    </Method> 

 

11 </Batch>

 

更新folder的item的xml格式

 

view sourceprint?01 <Batch OnError="Continue" PreCalc="TRUE" 

 

02 ListVersion="0" 

 

03 ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}"> 

 

04    <Method ID="1" Cmd="Update"> 

 

05       <Field Name="ID">3</Field> 

 

06       <Field Name="owshiddenversion">1</Field> 

 

07       <Field Name="FileRef"> 

 

08          http://Server/[sites/][Site/]Shared  

 

09          Documents/Folder</Field> 

 

10       <Field Name="FSObjType">1</Field> 

 

11       <Field Name="BaseName">Name</Field> 

 

12    </Method> 

 

13 </Batch>

 

更新documents的item的xml格式

 

view sourceprint?01 <Batch OnError="Continue" PreCalc="TRUE" 

 

02 ListVersion="0" 

 

03 ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}"> 

 

04    <Method ID="1" Cmd="Update"> 

 

05       <Field Name="ID">2</Field> 

 

06       <Field Name="owshiddenversion">1</Field> 

 

07       <Field Name="FileRef"> 

 

08          http://Server/[sites/][Site/]Shared  

 

09          Documents/File</Field> 

 

10       <Field Name="BaseName">Name</Field> 

 

11    </Method> 

 

12 </Batch>

 

我们更新item时xml格式的处理

 

view sourceprint?01 /// <summary> 

 

02 /// Get the approved XML Node that will be used for the insert method of the webservice 

 

03 /// </summary> 

 

04 /// <param name="batch"></param> 

 

05 /// <returns></returns> 

 

06 private XmlNode GetUpdateXmlNode(List<ListItemBE> batch) 

 

07 { 

 

08     StringBuilder xml = new StringBuilder(); 

 

09  

 

10     xml.Append(@"<Batch OnError=""Continue"">"); 

 

11     foreach (ListItemBE listItem in batch) 

 

12     { 

 

13         xml.AppendFormat(@"<Method ID=""{0}"" Cmd=""Update"">", listItem.Id); 

 

14         xml.Append(GetFieldInnerXml(listItem)); 

 

15         xml.Append("</Method>"); 

 

16     } 

 

17     xml.Append("</Batch>"); 

 

18  

 

19     //Get the Batch node 

 

20     XmlDocument doc = new XmlDocument(); 

 

21     doc.LoadXml(xml.ToString()); 

 

22  

 

23     XmlNode batchNode = doc.SelectSingleNode("//Batch"); 

 

24     return batchNode; 

 

25 }

 

更新item时调用sharepoint的web service的处理

 

view sourceprint?01 /// <summary> 

 

02 /// Insert the items 

 

03 /// </summary> 

 

04 /// <param name="batch"></param> 

 

05 /// <returns></returns> 

 

06 private UpdateResultBE UpdateItems(List<ListItemBE> batch) 

 

07 { 

 

08     //Get the Insert XML Node 

 

09     XmlNode batchNode = GetUpdateXmlNode(batch); 

 

10     XmlNode result = null; 

 

11  

 

12     _logger.Log("Started batch updating list Items"); 

 

13     try

 

14     { 

 

15         //Call the webservice 

 

16         result = _ws.UpdateListItems(_listProperty.ListName, batchNode); 

 

17     } 

 

18     catch (Exception ex) 

 

19     { 

 

20         _logger.Log(String.Format("Error updating Items. Exception: {0}. Stack Trace: {1}", ex.Message, ex.StackTrace)); 

 

21     } 

 

22  

 

23     _logger.Log("Finished batch updating list items"); 

 

24  

 

25     //Transform the result into an object 

 

26  

 

27     UpdateResultBE insertResult = new UpdateResultBE(result, _listProperty); 

 

28     LogInsertResult(insertResult); 

 

29  

 

30     return insertResult; 

 

31 }

 

UserGroup.GetUserCollectionFromSite用法

 

首先建立web service的连接,代码如下

 

view sourceprint?1 public static SPUserGroupWS.UserGroup GetUserGroupWebService(ListBE listProperty) 

 

2 { 

 

3     string wsUrl = GetWSUrl(listProperty) + "_vti_bin/usergroup.asmx"; 

 

4     SPUserGroupWS.UserGroup ws = new SPUserGroupWS.UserGroup(); 

 

5     ws.Url = wsUrl; 

 

6     ws.UseDefaultCredentials = true; 

 

7     ws.Credentials = WSHelper.GetCredentials(listProperty.Type); 

 

8     return ws; 

 

9 }

 

我们只取两个简单的字段做个示范

 

view sourceprint?1 public class UserInfoBE 

 

2 { 

 

3     public string ID { get; set; } 

 

4     public string NTLoginName { get; set; } 

 

5 }

 

获取本站点用户列表的方法

 

view sourceprint?01 public List<UserInfoBE> GetUserInfoList() 

 

02 { 

 

03     List<UserInfoBE> userInfoList = new List<UserInfoBE>(); 

 

04     UserInfoBE listItem; 

 

05  

 

06     XmlNode nodes = ws.GetUserCollectionFromSite(); 

 

07     foreach (XmlNode node in nodes) 

 

08     { 

 

09         if (node.Name == "Users") 

 

10         { 

 

11             for (int i = 0; i < node.ChildNodes.Count; i++) 

 

12             { 

 

13                 if (node.ChildNodes[i].Name == "User") 

 

14                 { 

 

15                     listItem = new UserInfoBE(); 

 

16                     listItem.ID = node.ChildNodes[i].Attributes["ID"].Value; 

 

17                     listItem.NTLoginName = node.ChildNodes[i].Attributes["LoginName"].Value; 

 

18                     userInfoList.Add(listItem); 

 

19                 } 

 

20             } 

 

21         } 

 

22     } 

 

23     return userInfoList; 

 

24 }

 

这样就可以通过web service获取站点用户列表信息,但要注意的是,web service的用户名和密码必须是sharepoint站点的管理员.

 

总结

 

学会sharepoint的web service用户对在做sharepoint站点的数据迁移时很有帮助。