SharePoint WebService服务

先获取凭据,才能远程操作

SharePoint内置了一套相对比较完整的WebServices提供给开发者,这样就可以在客户端、跨平台的程序中读取甚至修改SharePoint中的内容。不过当SharePoint网站不允许匿名访问的时候,调用WebServices自然也需要提供身份验证。

  SharePoint身份验证最常用的是Windows验证(WindowsAuthentication)和表单验证(FormAuthentication)两种。

 表单认证是SharePoint2007新加入的一种身份认证方式,其后台是使用.NetFramework中的Membership机制进行用户身份的识别,对于外网来说,大都是使用表单认证的方式来实现的。在使用表单认证的SharePoint网站中通过WebService获取数据稍微麻烦一些,不过也可以通过SharePoint提供的表单认证WebService来创建用户身份相关的Cookie。

  表单认证WebService的地址是:http://[server]/[site]/_vti_bin/authentication.asmx

Windows验证:

IDCardHelp.listService.Url = url; IDCardHelp.listService.Credentials = new NetworkCredential(user, pwd); //System.Net.CredentialCache.DefaultCredentials;


表单认证

 auth.Authentication myAuth = new auth.Authentication();

                myAuth.CookieContainer = new CookieContainer();

                myAuth.AllowAutoRedirect = true;

                auth.LoginResult lr = myAuth.Login(user, pwd);

                if (lr.ErrorCode == auth.LoginErrorCode.NoError)
                {
                    IDCardHelp.listService.CookieContainer = myAuth.CookieContainer;
                }

 

远程查询

  public  XmlNode Select(string listName, string query,string viewFields, int limitCount)
        {

            XmlDocument xmlDoc = new System.Xml.XmlDocument();
            XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
            XmlNode ndViewFields =
                xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
            XmlNode ndQueryOptions =
                xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
            ndQueryOptions.InnerXml =
                "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>";
            ndViewFields.InnerXml = viewFields;// "<FieldRef Name='CF'/><FieldRef Name='CFAddMarks'/>";
            ndQuery.InnerXml = query;

            try
            {
                return listService.GetListItems(listName, null, ndQuery,
                    ndViewFields,limitCount>0?limitCount.ToString():null, ndQueryOptions, null);
            }
            catch (Exception ex)
            {
            }
            return null;
        }

 增删改

 public XmlNode Crud(string listName,string strBatch)
        {
            
                XmlDocument xmlDoc = new XmlDocument();
                XmlElement elBatch = xmlDoc.CreateElement("Batch");
                elBatch.SetAttribute("OnError", "Continue");
                elBatch.InnerXml = strBatch;
                return listService.UpdateListItems(listName, elBatch);
          

        }

 处理返回的数据

 public void test(string listname, string query, string locationField)//
        {
           
                
                XmlNode ndListItems = IDCardHelp.Instance.Select(listname, query, "<FieldRef Name='" + locationField + "'/><FieldRef Name='ID'/><FieldRef Name='" + Info.strFieldJfkm + "'/>", 1);
                if (ndListItems == null)
                {
                    return null;
                }
                int count = int.Parse(ndListItems.SelectSingleNode("//@ItemCount").Value);
                if (count > 0)
                {
                    XmlNode nodeListLocation = ndListItems.SelectSingleNode("//@ows_" + locationField);
                    XmlNode nodeListJfkm = ndListItems.SelectSingleNode("//@ows_" + Info.strFieldJfkm);
                    XmlNode nodeListID = ndListItems.SelectSingleNode("//@ows_ID");//ID
                 }
        }

 

 /// <summary>
        /// 返回第一行节点
        /// </summary>
        /// <param name="ListItems"></param>
        /// <returns></returns>
        private XmlNode GetOneXmlNode(XmlNode ListItems)
        {
            IEnumerator ienum = ListItems.GetEnumerator();
            XmlNode current=null, dataRow = null;
            bool flag = false;
            while (ienum.MoveNext())
            {
                current = (XmlNode)ienum.Current;
                if (current.Name == "rs:data")
                {
                    dataRow = current;
                    ienum = dataRow.GetEnumerator();
                    while (ienum.MoveNext())
                    {
                        current = (XmlNode)ienum.Current;
                        if (current.Name == "z:row")
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                        break;
                }
            }
            return current;

        }
      
        [Obsolete]
        public void UpdateLeftTime(string uniqueId)
        {
           
           //今天该卡至少刷过一次,找到该项的ID,才能更新
            string strBatch = "<Method ID='1' Cmd='Update'>" +//cmd参数,Update/New、Delete
"<Field Name='ID'>" + uniqueId + "</Field><Field Name='" + Info.strFieldEndTime + "'>" + DateTime.Now.ToString(@"yyyy-MM-dd\THH:mm:ss\Z") + "</Field></Method>";
          
                IDCardHelp.Instance.Crud(Info.strListCheck, strBatch);
          
          
           
        }

 

posted on 2015-08-25 15:42  !无名之辈  阅读(289)  评论(0)    收藏  举报