maguy
maguy

Reputation: 1699

SharePoint 2007 c# need to get a document library (list name) from url, looking how to pass list name to GetListItems

Trying to programmatically get a list name from a url, then looking to pass the list name to the web service Lists (_vti_bin/Lists.asmx) in order to use the GetListItems or even use the Webs service to use GetViewCollection passing in the list name. Issue is I am able to get the list name as it shows in the title, but when passing it to either GetListItems or GetViewCollection it complains about incorrect GUID.

Example:

Sample url:

http://test.com/List Name Here/

I'm able to parse out "List Name Here" by using the Views service but when passing in "List Name Here" to GetListItems it fails with message regarding incorrect GUID. I guess I'm looking to see if this is possible and if so what format does the list name need to be internally to be able to pass to the GetListItems method?

EDIT

Just discovered one of the issues is SharePoint does some parsing of the list name int the url, removes commas, brackets, periods and dashes so the list name in my case had a period in the name which was striped out. So I might have to tell the end users to follow a naming convention unless there is any other way to get the full unedited list name.

Upvotes: 1

Views: 2808

Answers (1)

maguy
maguy

Reputation: 1699

Was able to get the actual list name by looping through the list collection (GetListCollection().ChildNodes). This allowed me to use the "DefaultViewUrl" and "Name" attributes to pass the correct list name to the GetListItems web service method. This allows the end user to pass in a url from SharePoint and the processing will determine the list name so it doesn't have to be provided in a config files etc.

Code below shows the basics to determine the list name from a url;

string name = string.Empty;
foreach (XmlNode ls in list.GetListCollection().ChildNodes)
{
    //Check whether list is document library
    if (Convert.ToInt32(ls.Attributes["ServerTemplate"].Value) != 0x65)
    {
        continue;
    }

    string defaultViewUrl = Convert.ToString(ls.Attributes["DefaultViewUrl"].Value);

    if (defaultViewUrl.Contains(listName))
    {
        name = ls.Attributes["Name"].Value;
        break;
    }
}

XmlNode ndListItems = list.GetListItems(name, null, ndQuery, ndViewFields, null, ndQueryOptions, null);
XmlNodeList oNodes = ndListItems.ChildNodes;

// rest of processing below...

Upvotes: 2

Related Questions