How can I count Document library in Sites(SPWeb) Level?
-
11-12-2020 - |
Question
I`m making SharePoint site Analysis program.
And I have a question.
In Sites(SPWeb) Level, I want to count document library.
So I`m using SPWeb.DocTemplate Property like this.
public class WebInfos
{
public void GetWebInfos(SPWeb web, ref ListInfos listinfos)
{
listinfos.SiteDocumentLibCount = web.DocTemplates.Count.ToString();
}
}
But the all sites result is always same...
Am I using wrong property? or make a method??
Please somebody help me
Solution
No need for looping , just get the library at a specific site based on the base type equal to DocumentLibrary
then get count of collection as the following :
using(SPSite site = new SPSite("https://yoursiteURL"))
{
using (SPWeb web = site.OpenWeb())
{
SPListCollection libcol = Web.GetListsOfType(SPBaseType.DocumentLibrary);
libcol.Count;
}
}
OTHER TIPS
Get the document library count using LINQ:
SPSite site = new SPSite("siteURL");
SPWeb web = site.OpenWeb();
var totalDocLibrary = (from SPList lst in web.Lists
where (lst.BaseType.Equals(SPBaseType.DocumentLibrary) && !lst.Hidden)
select lst).Count();
using(SPSite site = new SPSite("https://sitecollectionurl"))
{
using (SPWeb web = site.OpenWeb())
{
int i = 0;
foreach (SPList list in web.Lists)
{
if (list.BaseType == SPBaseType.DocumentLibrary)
{
i++;
}
}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange