C. Ross
C. Ross

Reputation: 31848

Programmatically access SharePoint lists and subsites?

How can I iterate SharePoint lists and subsites from a C# program? Is the SharePoint.dll from a SharePoint installation required for this, or is there a "Sharepoint client" dll available for remotely accessing that data?

Upvotes: 0

Views: 6310

Answers (3)

Hambone
Hambone

Reputation: 16397

I happen to be dealing with this very thing now... this works. I've dumbed down the code a bit to focus on just the mechanics. It's rough around the edges, but hopefully you get the idea. It's working for me.

Also, be sure to set up a web reference using the URL of your Sharepoint site. Use that as your "web reference" below.

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

Also, the SpFieldName method is not iron-clad. It's just a good guess, for most field names in a list. This, unfortunately, is a journey of discovery. You need to expose the XML to find the actual field names if they don't match.

Good hunting.

Upvotes: 0

David
David

Reputation: 163

For Sharepoint 2007 you will need to access the web services. In Sharepoint 2010, there is a sharepoint client object model.

http://msdn.microsoft.com/en-us/library/ee857094%28office.14%29.aspx

Upvotes: 1

Assaf
Assaf

Reputation: 1346

Use the Sharepoint web services; in particular the Webs and Lists webservices do what you ask.

For Sharepoint 2007: http://msdn.microsoft.com/en-us/library/bb862916(v=office.12).aspx

Upvotes: 6

Related Questions