2010-12-13 145 views
0

如何从C#程序迭代SharePoint列表和子网站?是否需要为此安装SharePoint安装SharePoint.dll,或者是否存在可用于远程访问该数据的“Sharepoint客户端”dll?以编程方式访问SharePoint列表和子网站?

+0

这是SharePoint 2007还是2010? – 2010-12-13 17:12:48

回答

0

我碰巧处理现在这事......这工作。我已经把代码搞糊涂了,只关注机制。这是边缘粗糙,但希望你能明白。它为我工作。

此外,请务必使用您的Sharepoint网站的URL设置Web引用。将其用作下面的“Web参考”。

private <web reference> _Service; 
    private String _ListGuid, _ViewGuid; 

    private Initialize() 
    { 
     _Service = new <web reference>.Lists(); 
     _Service.Credentials = System.Net.CredentialCache.DefaultCredentials; 
     _Service.Url = "https://sharepointsite/_vti_bin/lists.asmx"; 
    } 

    private String SpFieldName(String FieldName, Boolean Prefix) 
    { 
     return String.Format("{0}{1}", Prefix ? "ows_" : null, 
      FieldName.Replace(" ", "_x0020_")); 
    } 

    private String GetFieldValue(XmlAttributeCollection AttributesList, 
     String AttributeName) 
    { 
     AttributeName = SpFieldName(AttributeName, true); 
     return AttributesList[AttributeName] == null ? 
      null : return AttributesList[AttributeName].Value; 
    } 

    public void GetList() 
    { 
     string rowLimit = "2000"; // or whatever 

     System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); 
     System.Xml.XmlElement query = xmlDoc.CreateElement("Query"); 
     System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields"); 
     System.Xml.XmlElement queryOptions = 
      xmlDoc.CreateElement("QueryOptions"); 

     queryOptions.InnerXml = ""; 
     System.Xml.XmlNode nodes = _Service.GetListItems(_ListGuid, _ViewGuid, 
      query, viewFields, rowLimit, null, null); 

     foreach (System.Xml.XmlNode node in nodes) 
     { 
      if (node.Name.Equals("rs:data")) 
      { 
       for (int i = 0; i < node.ChildNodes.Count; i++) 
       { 
        if (node.ChildNodes[i].Name.Equals("z:row")) 
        { 
         XmlAttributeCollection att = 
          node.ChildNodes[i].Attributes; 
         String title = GetFieldValue("Title"); 
         String partNumber = GetFieldValue("Part Number"); 
        } 
       } 
      } 
     } 
    } 
} 

此外,SpFieldName方法不是铁包覆。对于列表中的大多数字段名称,这只是一个很好的猜测。不幸的是,这是一个发现之旅。如果它们不匹配,则需要公开XML以查找实际的字段名称。

好狩猎。

相关问题