现在的位置: 首页 > 综合 > 正文

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

2013年02月27日 ⁄ 综合 ⁄ 共 3741字 ⁄ 字号 评论关闭

概要

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

Lists.UpdateListItems的用法

更新普通list的item的xml格式

<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格式

<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格式

<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格式的处理

        /// <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的处理

        /// <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的连接,代码如下

        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;
        }

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

    public class UserInfoBE
    {
        public string ID { get; set; }
        public string NTLoginName { get; set; }
    }

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

        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/

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

抱歉!评论已关闭.