locked
Retrieve the list of document libraries? RRS feed

  • Question

  • How can I retrieve the names and properties of document libraries in a particular site using web services. I've seen the getListCollection method but it returns all the lists. How do I then differentiate between lists and document libraries?
    Thursday, April 5, 2007 12:31 PM

Answers

  •  John Dorian wrote:
    Thanks a lot man. Now I would like to retrieve only the document libraries on which the user has write access i.e. those in which the user can upload files. How can do this through web services?

     

    I'm not too sure about the best way to do this. Here is an example that uses the Permissions web service:

     

    Code Snippet

    PermissionsService.Permissions permissionsProxy = new PermissionsService.Permissions();

    permissionsProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

    XmlNode node = permissionsProxy.GetPermissionCollection("My Document Library", "List");

     

    // ExtractPermissionMask parses the node and removes the Mask attribute for the given user

    int permissionMask = ExtractPermissionMask(node, username);

     

    SPBasePermissions existingPermissions = (SPBasePermissions)permissionMask;

    bool canUpload = (existingPermissions & SPBasePermissions.AddListItems) == SPBasePermissions.AddListItems;

     

    The xml in the XmlNode is going to look something like: (From VS so there are escape characters in there). I omitted the code (because I didn't bother to write any Surprise ) in ExtractPermissionMask which returns the value of the Mask attribute of the appropriate Permission element.

     

    "<Permissions xmlns=\"http://schemas.microsoft.com/sharepoint/soap/directory/\"><Permission MemberID=\"3\" Mask=\"138612801\" MemberIsUser=\"False\" MemberGlobal=\"True\" GroupName=\"Viewers\" /><Permission MemberID=\"4\" Mask=\"-1\" MemberIsUser=\"False\" MemberGlobal=\"True\" GroupName=\"My Site Owners\" /><Permission MemberID=\"5\" Mask=\"138612833\" MemberIsUser=\"False\" MemberGlobal=\"True\" GroupName=\"My Site Visitors\" /><Permission MemberID=\"6\" Mask=\"1011028719\" MemberIsUser=\"False\" MemberGlobal=\"True\" GroupName=\"My Site Members\" /><Permission MemberID=\"8\" Mask=\"134287360\" MemberIsUser=\"True\" MemberGlobal=\"False\" UserLogin=\"MYMACHINE\\testAccount\" /><Permission MemberID=\"1073741823\" Mask=\"134287360\" MemberIsUser=\"True\" MemberGlobal=\"False\" UserLogin=\"SHAREPOINT\\system\" /></Permissions>"

     

    Things to note:

    I'm assuming the Mask attribute corresponds to SPBasePermissions.

    I'm assuming SPBasePermissions.AddListItems is the appropriate permission for uploading to a document library.

    The first parameter of the GetPermissionCollection is the name of a List/Document Library. You'll have to know this beforehand or get it by hitting the Lists web service.

    Friday, April 6, 2007 9:26 AM

All replies

  • Document libraries have a template ID of 101. you can use that...
    Friday, April 6, 2007 4:40 AM
  •  Ishai Sagi wrote:
    Document libraries have a template ID of 101. you can use that...

     

    So here's some code (I've no idea how to format it properly, and code is being split onto different lines, but you get the idea):

     

    Code Snippet

    // Whatever your sharepoint webreference is. I called it ListsService

    ListsService.Lists listsServiceProxy = new ListsService.Lists();

    listsServiceProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

    System.Xml.XmlNode listsCollection = listsServiceProxy.GetListCollection();

    foreach (XmlNode listNode in listsCollection.ChildNodes)

    {

    int serverTemplate = Int32.Parse(listNode.Attributes["ServerTemplate"].Value);

    if (serverTemplate == 101)

    {

    // do stuff

    }

    }

     

    I'm hoping there's a better way to extract document libraries using web services, but so far this is what I'm doing too.

    Friday, April 6, 2007 5:33 AM
  • well, you should use xpath instead of iterating over all the nodes.

     

    Friday, April 6, 2007 5:43 AM
  • Thanks a lot man. Now I would like to retrieve only the document libraries on which the user has write access i.e. those in which the user can upload files. How can do this through web services?
    Friday, April 6, 2007 6:59 AM
  •  John Dorian wrote:
    Thanks a lot man. Now I would like to retrieve only the document libraries on which the user has write access i.e. those in which the user can upload files. How can do this through web services?

     

    I'm not too sure about the best way to do this. Here is an example that uses the Permissions web service:

     

    Code Snippet

    PermissionsService.Permissions permissionsProxy = new PermissionsService.Permissions();

    permissionsProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

    XmlNode node = permissionsProxy.GetPermissionCollection("My Document Library", "List");

     

    // ExtractPermissionMask parses the node and removes the Mask attribute for the given user

    int permissionMask = ExtractPermissionMask(node, username);

     

    SPBasePermissions existingPermissions = (SPBasePermissions)permissionMask;

    bool canUpload = (existingPermissions & SPBasePermissions.AddListItems) == SPBasePermissions.AddListItems;

     

    The xml in the XmlNode is going to look something like: (From VS so there are escape characters in there). I omitted the code (because I didn't bother to write any Surprise ) in ExtractPermissionMask which returns the value of the Mask attribute of the appropriate Permission element.

     

    "<Permissions xmlns=\"http://schemas.microsoft.com/sharepoint/soap/directory/\"><Permission MemberID=\"3\" Mask=\"138612801\" MemberIsUser=\"False\" MemberGlobal=\"True\" GroupName=\"Viewers\" /><Permission MemberID=\"4\" Mask=\"-1\" MemberIsUser=\"False\" MemberGlobal=\"True\" GroupName=\"My Site Owners\" /><Permission MemberID=\"5\" Mask=\"138612833\" MemberIsUser=\"False\" MemberGlobal=\"True\" GroupName=\"My Site Visitors\" /><Permission MemberID=\"6\" Mask=\"1011028719\" MemberIsUser=\"False\" MemberGlobal=\"True\" GroupName=\"My Site Members\" /><Permission MemberID=\"8\" Mask=\"134287360\" MemberIsUser=\"True\" MemberGlobal=\"False\" UserLogin=\"MYMACHINE\\testAccount\" /><Permission MemberID=\"1073741823\" Mask=\"134287360\" MemberIsUser=\"True\" MemberGlobal=\"False\" UserLogin=\"SHAREPOINT\\system\" /></Permissions>"

     

    Things to note:

    I'm assuming the Mask attribute corresponds to SPBasePermissions.

    I'm assuming SPBasePermissions.AddListItems is the appropriate permission for uploading to a document library.

    The first parameter of the GetPermissionCollection is the name of a List/Document Library. You'll have to know this beforehand or get it by hitting the Lists web service.

    Friday, April 6, 2007 9:26 AM
  • Ok, now that I have the list of document libraries I'd like to retrieve their names etc. of their fields because I need to upload documents into them. I can get the schema of the document library by the web service method called getList but it contains all the columns. How do I filter for the ones that only show up in the web view of the document library. Any idea?
    Saturday, April 7, 2007 8:03 PM