2010-12-13 158 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以查找實際的字段名稱。

好狩獵。

相關問題