Saturday, August 6, 2011

Programmatically change Workflow Auto Clean up days

Well, if you associate a workflow to any sharepoint list/ library & if the workflow instance is completed, then its association with its tasks and history list details will lasts for only 60 days. This is because we have a sharepoint out of the box "Workflow Auto clean up" timer job running daily for every webapplication.

If you want to change the default 60 days of clean up, then you are the right stop. The below sample will help you out to achieve the behaviour.

static void Main()
        {
            using (SPSite site = new SPSite("http://site_URL"))
            {
              usingSPWeb web = site.OpenWeb())
              {
                SPWorkflowTemplateCollection collection = web.WorkflowTemplates;
                SPList list = web.Lists["LIST_NAME"];//List name
                SPWorkflowAssociation _asso = null;
                foreach (SPWorkflowAssociation asso in list.WorkflowAssociations)
                {
                    if (asso.Name == "WORKFLOW_NAME")//Workflow name
                    {
                        asso.AutoCleanupDays = 7;//clean up days
                        _asso = asso;
                    }
                }
               list.UpdateWorkflowAssociation(_asso);
            }}
        }

Happy Coding !!!!!

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 :)

Thursday, March 3, 2011

How to get list items through Sharepoint Object Model

Programmatically we can get sharepoint list items through Object Model.
It is so easy.

Just we need SPSite, SPWeb instances and get the list out of it. Then we need to formulate SPQuery and get the items.

There you go.

using(SPSite site=new SPSite("Site_URL")
{
   using(SPWeb web=site.OpenWeb())
   {
    SPList list = web.Lists["List_Name"];
    SPQuery query = new SPQuery();
    query.ViewAttributes = "Scope=\"Recursive\"";// this will search your folders too
    SPListCollection newsListColl = list.GetItems(query);
    }
}