Permission error on SPWeb.Lists when using in custom web service


  • Hello,

    I am new to Sharepoint. I have been assigned to create a Sharepoint Custom Web Service as explain in this Blog. In this web service, I need to iterate shared documents of all sub sites and return a list of required documents. Here is my code which I am using in Web Service method

    SPWebApplication webApp = SPContext.Current.Site.WebApplication;
                    #region Traverse all sub sites
                    foreach (SPSite subSite in webApp.Sites)
                        using (SPWeb sWeb = subSite.OpenWeb())
    				foreach (SPList list in sWeb.Lists)
    					if (list.BaseType == SPBaseType.DocumentLibrary &&
                                   			list.Hidden == false && list.BaseTemplate == SPListTemplateType.DocumentLibrary &&
                                    		list.Title.Equals("Style Library", StringComparison.InvariantCultureIgnoreCase) == false &&
                                    		list.Title.Equals("Site Assets", StringComparison.InvariantCultureIgnoreCase) == false)

    At this line, I am getting following exception  ('System.Threading.ThreadAbortException')

    foreach (SPList list in sWeb.Lists)

    I believe this is related to permission error but I don't know how to solve it. As my Web Service is deploying under sharepoint foundation (_vti_bin folder) and its DLL is in GAC.

    When I am consuming this web service method in Console application running on same machine where Sharepoint installed, I get following error:

    The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

    Please help.

    Thanks in advance.

    M. Irfan

    • Upravený M. Irfan 11. června 2012 20:53 mentioned client side error also.
    11. června 2012 20:40

Všechny reakce

  • HI

    I'd suggest that you use CAML with SPSiteDataQuery instead. If you then set Servertemplate to 101 you'll get all Shared Documents + (other possible doc libraries that you have to filter out).

    Kind Regards Bjoern

    11. června 2012 21:24
  • The code is timing out. The looping through all the sites and lists is taking too long. I recommend finding an alternative to the looping depending on what you are doing. What are you exactly doing in your code. It is possible to accomplish what you are doing with the SPSiteDataQuery class like Bjoern stated but there are limits to that also.

    Blog | SharePoint Field Notes Dev Tool | ClassMaster

    12. června 2012 2:03
  • Thank you for your comments Steve & Bjoern. My code is not timing out. As if instead of looping on Lists if I just try to get total lists count using following line of code

    int listCount = sWeb.Lists.count;

    Still it give the same exception which seems the problem persist whenevery I tried to call "sWeb.Lists". I even also tried to access it using following code

    SPListCollection listCollection = null;
    int listcount = 0;
                   listCollection = sWeb.GetListsOfType(SPBaseType.DocumentLibrary); ;
                  listcount = listCollection.Count;
    but still same problem. I also debug this service after attaching w3p process in VS and find that as soon as I access Lists

    Moreover I also tried to use the exactly same code example of using SPSiteDataQuery as given on this page but still get following exception (Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.) on the following line.

    DataTable dt = web.GetSiteData(query);
    Please let me know if you need further detail. I am sure, I must be missing something in configuration of Web Service. Please help.

    M. Irfan

    12. června 2012 9:29
  • Hey,

    Is there a reason that you're deploying the service in SharePoint as opposed to just creating a normal web service?

    Also in the code, don't use 

    foreach (SPList list in sWeb.Lists)

    It queries the sWeb.Lists on every iteration, rather use

    var spLists = sWeb.Lists;
    foreach (SPList list in spLists)


    I find it distasteful to beg for 'Mark as Answer' and 'Mark as helpful'. It's supposed to be about helping people, not about getting the high score.

    12. června 2012 10:34
  • Hello,

    Although I am new in Sharepoint but I am interested to create service in Sharepoint foundation so that we dont need to create a separate application directory in IIS. let me share my exact requirements so that you can give me any idea, if you feel it more better.

    I am creating a new module in my existing application suite (.Net 4.0 framework based) and in this module I need to process all shared documents available in all site collection of Sharepoint.

    I could not use Sharepoint Server Objects as these are (.Net 3.5 based) even my application will always work on the same machine where Sharepoint installed. So I thought, I should use Web Services which return the list of documents (name & url path).

    I will thankful if you can provide me any sample code or point me to any link from where I can get any idea.

    M. Irfan

    12. června 2012 11:38
  • Hello Everyone,

    Please help me on it.

    M. Irfan

    14. června 2012 8:01