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

Wednesday, May 15, 2013

Deleting all versions of a document present in document library except last 2 major and 1 minor versions Moss2007/Sharepoint2010

Recently i came across a requirement to develop a admin component which will delete all the versions of the document except some major and minor verisons present in a Sharepoint document library. Thought of sharing with you guys aswell.
In this post we are going to Delete all versions of a document present in document library except last 2 major and 1 minor versions
Below is the code snippet which performs the action.
You can made the variables int majorVersionsRequired and int minorVersionsRequired are as configurable depending on the versions you require.

try
{
   using (SPSite site = new SPSite("<<http://siteurl/>>"))
   {
      using (SPWeb web = site.OpenWeb())
      {
         SPList list = web.Lists["<<Doc Lib Name>>"];
         int majorVersionsRequired = 2;
         int minorVersionsRequired = 1;

         foreach (SPListItem item in list.Items)
         {
            SPListItemVersionCollection versionCol = item.Versions;
            int count = versionCol.Count;
            int tempMajorVersionsRequired = 0;
            int tempMinorVersionsRequired = 0;
            int versionSiNo = 0;
                           
            for (int i = 0; i <= count - 1; i++)
            {
               if (versionCol[i - versionSiNo].Level == SPFileLevel.Published)
               {
                  // this is a major version
                  tempMajorVersionsRequired++;
                  if (tempMajorVersionsRequired > majorVersionsRequired)
                  {
                     SPListItemVersion version = versionCol[i - versionSiNo];
                     version.Delete();
                     versionSiNo++;
                  }
               }
               else if (versionCol[i - versionSiNo].Level == SPFileLevel.Draft)
               {
                  //this is a minor version
                  tempMinorVersionsRequired++;
                  if (tempMinorVersionsRequired > minorVersionsRequired)
                  {
                     SPListItemVersion version = versionCol[i - versionSiNo];
                     version.Delete();
                     versionSiNo++;
                  }
               }
            }
          } 
      }
   }
}
catch (Exception ex)
{

}


Hope this helps...

Saturday, February 23, 2013

TreeView with the nodes as document library names using Sharepoint2010


In this post we are going to construct a TreeView with the nodes as sharepoint document library names using Sharepoint2010
Root Node will be the Site Url. 

Before we proceed with the code add a treeview with ID as myTreeView to your page. 
Below is the code snippet.

try
            {
                using (SPSite site = new SPSite("<<http://site Url>>"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPListCollection docLibraryColl = web.GetListsOfType(SPBaseType.DocumentLibrary);

                        List<string> docLibrariesList = new List<string>();
                        foreach (SPList list in docLibraryColl)
                        {
                            if (!list.Hidden)
//if you don’t want style library and Forms templates then use below condition instead of above
                            //if (!list.Hidden && list.title != "Style Library" && list.title != "Form Templates")
                            {
                                SPDocumentLibrary doclib = (SPDocumentLibrary)list;
                                string docLibTitle = doclib.Title;
                                int docsCount = doclib.ItemCount;
                                docLibrariesList.Add(docLibTitle);
                            }
                        }

                        TreeNode RootNode = new TreeNode();
                        //Here we are binding the root node as Site Url
   RootNode.Text = "<<http://site url>>";

                        foreach (string docLibrary in docLibrariesList)
                        {
                            TreeNode childNode = new TreeNode();
                            childNode.Text = docLibrary;
                            RootNode.ChildNodes.Add(childNode);
                        }
   // myTreeView is treeview controls ID present on the aspx page
                        myTreeView.Nodes.Add(RootNode);

                    }
                }
            }
            catch (Exception ex)
            {

            }


Hope this helps..

Popular Posts