Thursday, September 3, 2015

Get DocLibrary Size using Csom SharePoint

 public static string getDocLibraySize(string Context, string DocLibraryName)
        {
            double docLibSize = 0;
            try
            {
                using (ClientContext ctx = new ClientContext(Context))
            {
               


               // ctx.Credentials = new NetworkCredential("userid", "paswword", "domain");
                List oList = ctx.Web.Lists.GetByTitle(DocLibraryName);
                CamlQuery oQuery = new CamlQuery();
//get all files exclude the folders and bring all items
                oQuery.ViewXml = "<View Scope='RecursiveAll'>"
   + "<Query>"
   + "   <Where>"
   + "      <Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>"
   + "   </Where>"
   + "</Query>"
   + "</View>";


                ListItemCollection collListItem = oList.GetItems(oQuery);
                ctx.Load(oList);
                ctx.ExecuteQuery();
                ctx.Load(collListItem);
                ctx.ExecuteQuery();
                double ListSize = 0;


                foreach (ListItem oListItem in collListItem)
                {
                    double TotalFileSize = 0;

                    Microsoft.SharePoint.Client.File file = oListItem.File;
                    ctx.Load(file);
                    ctx.ExecuteQuery();
                    FileVersionCollection versions = file.Versions;

                    ctx.Load(versions);
                    ctx.ExecuteQuery();
                    TotalFileSize = TotalFileSize + file.Length;
                    int fileCount = versions.Count;
                    if (fileCount >= 1)
                    {
                        if (versions != null)
                        {
                            double ItemVersionSize = 0;
                            foreach (FileVersion _version in versions)
                            {
                                ctx.Load(_version);
                                ctx.ExecuteQuery();
                                ItemVersionSize = ItemVersionSize + _version.Size;

                            }
                            TotalFileSize = TotalFileSize + ItemVersionSize;
                        }
                    }
                    ListSize = ListSize + TotalFileSize;


                }

                //Converting byte to MB and rounding to 2 decimal digits
                docLibSize = Math.Round((ListSize / (1024 * 1024)), 2);
               
                //webSize = DefineSizeWithUnitMeasure(ListSize, out unitMeasure);
                Console.WriteLine("Total DocLibrary Size : " + docLibSize.ToString());
            }
            }
            catch (Exception e)
            {

                Console.WriteLine("Unable to Get DocLibrarySize");
            }

            return docLibSize.ToString();
        }

        public static double DefineSizeWithUnitMeasure(double sizeInBytes, out string unitMeasure)
        {
            unitMeasure = " Bytes";
            double size = sizeInBytes;

            if (size > 1024)
            {
                size = sizeInBytes / 1024d;//KB
                unitMeasure = " KB";
            }
            if (size > 1024)
            {
                size = size / 1024d;//MB
                unitMeasure = " MB";
            }
            if (size > 1024)
            {
                size = size / 1024d; //GB
                unitMeasure = " GB";
            }

            if (size > 1024)
            {
                size = size / 1024d; //TB
                unitMeasure = " TB";
            }

            return size;
        }

No comments:

Post a Comment