4

Hi i`m making a Windows application that analyse the SharePoint Site.

In SPWeb level, I want to get the value of list count. I tried to like this

SPWeb.Lists.Count.ToString();

But when I compare with that value, it`s different, not matched...

So I want to try make code, unfortunately, I`m just beginner of SharePoint...

Please somebody help me. give some examples or links about that.

3 Answers 3

4

This is because of the SPWeb.Lists.Count.ToString(); return the number of all lists and libraries not only lists

To can get the accurate number of lists only you should exclude the library and other list types from your code as the following

int listcount=0;

foreach (SPList list in Web.Lists)
        {
            if (!list.Hidden & list.BaseType != SPBaseType.DocumentLibrary &
                (
                    (int)list.BaseTemplate != 109 |     // Picture Library
                    (int)list.BaseTemplate != 851       // Asset Library
                ))
            {
listcount++;
            }
        }

If you need to get only the count of custom lists, try the following :

int listcount=0;

foreach (SPList list in Web.Lists)
        {
            if ((int)list.BaseTemplate == 100 )
            {
       listcount++;
            }
        }

To check all BaseTemplateID check Get the RegistrationId template list for a SharePoint custom action via Power Shell.

1
  • Thank you M.Qassas. I made a code using your source! :) Now it works well exactly what I want!
    – Ryden Choi
    Commented Dec 20, 2016 at 5:43
6

SPWeb.Lists.Count.ToString(); is correct way to find number of Lists for any SPWeb. You might be seeing in difference in count you see here and in Site is because SharePoint has couple of hidden list like UserInformation list, managed metadata list which are hidden from UI. Also we can hide our custom list by using powershell/server side code.

To give an idea and to identify total number of Hidden list, you can iterate through above Lists and match it with what your list on sites.

3
  • So there is another way to get list count?? for example, get by listweb id
    – Ryden Choi
    Commented Dec 19, 2016 at 7:57
  • no..whatever you do, List on SPWeb will retrieve all lists including hidden ones, you need to seperate the count manually. can you tell me what is your purpose of get this list counts ? do you want to display anywhere to user ? Commented Dec 19, 2016 at 8:19
  • Oh I just want to count expect hidden lists
    – Ryden Choi
    Commented Dec 19, 2016 at 8:33
3

Try following code

 using (SPSite site = new SPSite("Web Url"))
  {
      using (SPWeb web = site.OpenWeb())
      {
          Console.Write(web.Lists.Count);
          Console.ReadLine();
      }
  }
4
  • @akshay...what he is trying and your answer is same.... Commented Dec 19, 2016 at 7:23
  • yeah this is the same my code
    – Ryden Choi
    Commented Dec 19, 2016 at 7:44
  • @Siddharth Vaghasia, Yes, you are write.My mistake. sorry.I thought code may have some issue so,I just put complete code based on the post title(How to get the value of list count in SPWeb level?). Commented Dec 19, 2016 at 8:13
  • Akshay It`s okay! And thank you for try to help me :)
    – Ryden Choi
    Commented Dec 19, 2016 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.