Showing posts with label Document Library. Show all posts
Showing posts with label Document Library. Show all posts

Monday, December 26, 2011

How to add folder to Sharepoint list programmatically

Here is the code.


using (SPSite siteCollection = new SPSite("http://siteURL/"))
            {
                using (SPWeb web = siteCollection.OpenWeb())
                {
                    SPList list = web.Lists["ListName"];
                    SPListItemCollection itemColl = list.Items;
                    SPListItem item = itemColl.Add("/Lists/ListName", SPFileSystemObjectType.Folder, null);
                    item["Title"] = "folderName";
                    item.Update();

                }
            }

Document version size in Sharepoint

The below code will calculate the document version size for each item for a single document library.

using (SPSite site = new SPSite("http://Site_URL/")) //Specify your SiteURL
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList objList = web.Lists["Doclib"];//Document library Name
                    SPQuery objQuery = new SPQuery();
                   
                    objQuery.ViewAttributes = "Scope=\"Recursive\"";

                    SPListItemCollection objItemColl = objList.GetItems(objQuery);
                    foreach (SPListItem objItem in objItemColl)
                    {
                        long versionSize = 0;

                        SPListItemVersionCollection objVerisionColl = objItem.Versions;

                        foreach (SPListItemVersion objVersion in objVerisionColl)
                        {
                            int versionID = objVersion.VersionId;
                            string strVersionLabel = objVersion.VersionLabel;

                            SPListItem objLstItm = objVersion.ListItem;
                            versionSize = versionSize + objLstItm.File.Length;
                           
                        }
                        Console.WriteLine("Item:" + objItem.Name + " Version Count:" + objVerisionColl.Count + " Size:" + versionSize.ToString());
                       

                    }
                }
            }

Saturday, August 6, 2011

SPFile.Versions.Count & SPListItem.Versions.Count

There is always a confusion between SPFile.Versions.Count & SPListItem.Versions.Count, as to which one to use and what is its purpose.
  

SPListItem.Versions.Count is always 1 more than SPFile.Versions.Count. The reason is SPFile consider the latest version as the current version and therefore does not include this in its version collection.

Thus, SPListItem.Versions.Count[1]  is equal to SPListItem.SPFile.Versions.Count[0].

if you go to  version history of a document , you will get a count equal to SPListItem.Versions.Count

Thus SPListItem.Versions.Count will give you, an exact version count of a document.


Sharepoint Document library - Access denied while reading draft

In your custom code, you might have written a logic to read all documents from a document library. Sometimes you might get access denied for some files in library.

The reason behind this is the version setting of the document library, especially when you have "major & minor" versioning setting enabled. When you track major and minor versions, the major versions are whole numbers, and the minor versions are decimals. Minor versions are the draft versions, major versions are the published ones.
For example, 0.1 is the first minor version of a file, 1.4 is the fourth minor version of a file that was published once, and 3.0 is the third major version of a published file.


The version is stored by default is a minor version. When you save a file and close it, the version is tracked as a minor version. Minor versions are considered drafts that are still being developed.
You must first publish the file in order for it to become a major version. You can publish the file by using drop-down commands in a library

In your API, if lock a sitecollection and then try to read your draft document, obviously you will get "access denied", meaning, the content DB is in read only mode when the site collection is locked


The reason behind this was that, documents were never published [they were in draft mode only]. So documents have to be published with at least one major version to prevent this error.

Wednesday, May 25, 2011

How to export the documents from sharepoint document library to hard drive

Sample to export the documents from sharepoint document library to hard drive

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using Microsoft.SharePoint.Administration;
namespace ExportDocuments
{
    class Exportdoc
    {
        private static void Main()
        {
            string strDownloadPath = @"C:\MyDownloadFolder\";//physical location
            string url = "http://Site_Url";
            using (SPSite oSiteCollection = new SPSite(url))
            {
                SPWebCollection collWebsites = oSiteCollection.AllWebs;
                Console.WriteLine("Websites Count: {0}", collWebsites.Count);
                Console.WriteLine("  ");
                string rootwebtitle = oSiteCollection.RootWeb.Title;
                foreach (SPWeb oWebsite in collWebsites)
                {
                    Console.WriteLine("Site : " + oWebsite.Title);
                    //Path to download the files
                    string websiteurl = oWebsite.Url;
                    string framedUrl = websiteurl.Replace(oSiteCollection.Url, "");
                    string path = strDownloadPath + "\\" + rootwebtitle + framedUrl.Replace('/', '\\');
                    Directory.CreateDirectory(path);
                    DownloadDocs(oWebsite.Url, path);
                    oWebsite.Dispose();
                    Console.WriteLine("--------------------");
                }
            }
            Console.WriteLine("Please click enter to exit");
            Console.ReadLine();
        }
       private static void DownloadDocs(string siteURL, string sitepath)
        {
            //List of OOTB document libraries - These doc libraries will not be downloaded
            List<string> ootbLib = new List<string>();
            ootbLib.Add("Converted Forms");
            ootbLib.Add("Form Templates");
            ootbLib.Add("Images");
            ootbLib.Add("List Template Gallery");
            ootbLib.Add("Master Page Gallery");
            ootbLib.Add("Pages");
            ootbLib.Add("Reporting Templates");
            ootbLib.Add("Site Collection Documents");
            ootbLib.Add("Site Collection Images");
            ootbLib.Add("Site Template Gallery");
            ootbLib.Add("Style Library");
            ootbLib.Add("Web Part Gallery");
            using (SPSite oSite = new SPSite(siteURL))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    foreach (SPList list in oWeb.Lists)
                    {
                        string actpath = sitepath + "\\" + list.Title+ "\\"  ;
                        if (list.BaseType == SPBaseType.DocumentLibrary)
                        {
                            if (!ootbLib.Exists(delegate(string p) { return p.Trim() == list.Title; }))
                            {
                                Console.WriteLine("Downloading from Library ::" + list.Title);
                                Directory.CreateDirectory(actpath);
                                TraverseAllListItems(list,actpath);
                            }
                        }
                    }
                }
            }
        }
        private static void TraverseAllListItems(SPList list,string path)
        {
            SPQuery qry = new SPQuery();
            qry.ViewAttributes = "Scope=\"Recursive\"";
           
            try
            {
                
                SPListItemCollection ic = list.GetItems(qry);
                foreach (SPListItem subitem in ic)
                {
                    string actpath = path + "\\" + subitem.Url;
                   
                    string itemurl = subitem.Url;
                    int index = itemurl.IndexOf("/");
                    string subpath = itemurl.Substring(index + 1);
                    string finalpath = path + subpath;
                    finalpath = finalpath.Replace(subitem.Name, "");
                    finalpath = finalpath.Replace("/", "\\");
                    Console.WriteLine(finalpath);
                    Directory.CreateDirectory(finalpath);
                    byte[] binFile = subitem.File.OpenBinary();
                    System.IO.FileStream fstream = System.IO.File.Create(finalpath + "\\" + subitem.Name);
                    fstream.Write(binFile, 0, binFile.Length);
                 
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
}
}
 Happy coding :)