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

概要

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

Lists.UpdateListItems的用法

更新普通list的item的xml格式

1
2
3
4
5
6
7
8
9
10
11
<Batch OnError="Continue" ListVersion="1"
ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
   <Method ID="1" Cmd="Update">
      <Field Name="ID">4<Field>
      <Field Name="Field_Name">Value</Field>
   </Method>
   <Method ID="2" Cmd="Update">
      <Field Name="ID" >6</Field>
      <Field Name="Field_Name">Value</Field>
   </Method>
</Batch>

更新folder的item的xml格式

1
2
3
4
5
6
7
8
9
10
11
12
13
<Batch OnError="Continue" PreCalc="TRUE"
ListVersion="0"
ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
   <Method ID="1" Cmd="Update">
      <Field Name="ID">3</Field>
      <Field Name="owshiddenversion">1</Field>
      <Field Name="FileRef">
         http://Server/[sites/][Site/]Shared
         Documents/Folder</Field>
      <Field Name="FSObjType">1</Field>
      <Field Name="BaseName">Name</Field>
   </Method>
</Batch>

更新documents的item的xml格式

1
2
3
4
5
6
7
8
9
10
11
12
<Batch OnError="Continue" PreCalc="TRUE"
ListVersion="0"
ViewName="{EF2F5A21-0FD0-4654-84ED-112B4F5A48F8}">
   <Method ID="1" Cmd="Update">
      <Field Name="ID">2</Field>
      <Field Name="owshiddenversion">1</Field>
      <Field Name="FileRef">
         http://Server/[sites/][Site/]Shared
         Documents/File</Field>
      <Field Name="BaseName">Name</Field>
   </Method>
</Batch>

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/// <summary>
/// Get the approved XML Node that will be used for the insert method of the webservice
/// </summary>
/// <param name="batch"></param>
/// <returns></returns>
private XmlNode GetUpdateXmlNode(List<ListItemBE> batch)
{
    StringBuilder xml = new StringBuilder();
 
    xml.Append(@"<Batch OnError=""Continue"">");
    foreach (ListItemBE listItem in batch)
    {
        xml.AppendFormat(@"<Method ID=""{0}"" Cmd=""Update"">", listItem.Id);
        xml.Append(GetFieldInnerXml(listItem));
        xml.Append("</Method>");
    }
    xml.Append("</Batch>");
 
    //Get the Batch node
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml.ToString());
 
    XmlNode batchNode = doc.SelectSingleNode("//Batch");
    return batchNode;
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// <summary>
/// Insert the items
/// </summary>
/// <param name="batch"></param>
/// <returns></returns>
private UpdateResultBE UpdateItems(List<ListItemBE> batch)
{
    //Get the Insert XML Node
    XmlNode batchNode = GetUpdateXmlNode(batch);
    XmlNode result = null;
 
    _logger.Log("Started batch updating list Items");
    try
    {
        //Call the webservice
        result = _ws.UpdateListItems(_listProperty.ListName, batchNode);
    }
    catch (Exception ex)
    {
        _logger.Log(String.Format("Error updating Items. Exception: {0}. Stack Trace: {1}", ex.Message, ex.StackTrace));
    }
 
    _logger.Log("Finished batch updating list items");
 
    //Transform the result into an object
 
    UpdateResultBE insertResult = new UpdateResultBE(result, _listProperty);
    LogInsertResult(insertResult);
 
    return insertResult;
}

UserGroup.GetUserCollectionFromSite用法

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

1
2
3
4
5
6
7
8
9
public static SPUserGroupWS.UserGroup GetUserGroupWebService(ListBE listProperty)
{
    string wsUrl = GetWSUrl(listProperty) + "_vti_bin/usergroup.asmx";
    SPUserGroupWS.UserGroup ws = new SPUserGroupWS.UserGroup();
    ws.Url = wsUrl;
    ws.UseDefaultCredentials = true;
    ws.Credentials = WSHelper.GetCredentials(listProperty.Type);
    return ws;
}

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

1
2
3
4
5
public class UserInfoBE
{
    public string ID { get; set; }
    public string NTLoginName { get; set; }
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public List<UserInfoBE> GetUserInfoList()
{
    List<UserInfoBE> userInfoList = new List<UserInfoBE>();
    UserInfoBE listItem;
 
    XmlNode nodes = ws.GetUserCollectionFromSite();
    foreach (XmlNode node in nodes)
    {
        if (node.Name == "Users")
        {
            for (int i = 0; i < node.ChildNodes.Count; i++)
            {
                if (node.ChildNodes[i].Name == "User")
                {
                    listItem = new UserInfoBE();
                    listItem.ID = node.ChildNodes[i].Attributes["ID"].Value;
                    listItem.NTLoginName = node.ChildNodes[i].Attributes["LoginName"].Value;
                    userInfoList.Add(listItem);
                }
            }
        }
    }
    return userInfoList;
}

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

总结

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

作者:spring yang

出处:http://www.cnblogs.com/springyangwc/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @   spring yang  阅读(2377)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示