Wednesday, March 10, 2010

Archive SharePoint Document Library versions.

Requirement : Archive versions created in a document library to another document library on daily basis.

Study :  There are vairuos option to try doing it, but to automate the process its better to go fir shareoint timer jobs.

Solution : Create timer jobs which starts on feature activation performing the requirement on daily basis.

  1.  Create Timer job in a feature.
         You can start creating a timer job and attaching it with feature from the below site steps.

        Only difference from the example is that you have to use SPDailySchedule

   2.  Write code for perform versioning in the document library

       public override void Execute (Guid contentDbId) {
             string[] urlofFile = null;
             string url = string.Empty;
             byte[] dBytes = null;
             StringBuilder verID = new StringBuilder(10);
             SPFileVersionCollection vercol = null;
             SPFile file = null;
         //Call webapplication
                   SPWebApplication webApplication = this.Parent as SPWebApplication;
                  SPContentDatabase contentDataBase = webApplication.ContentDatabases  [contentDBGuid];
          //call document librarys
   SPDocumentLibrary doclib1 = (SPDocumentLibrary)contentDataBase.Sites[0].RootWeb.Lists ["DocLib"];
   //Archival doc library
SPDocumentLibrary doclib2 = (SPDocumentLibrary)contentDataBase.Sites[0].RootWeb.Lists["Archives"];
//get current web
SPWeb currentweb = contentDataBase.Sites[0].RootWeb;
//get filecollection.
SPFileCollection filecols = currentweb.GetFolder("/DocLib").Files;
//Call Spfolder to add items to the archive.
SPFolder doclib2Folder = currentweb.Folders["/Archives"];
foreach (SPListItem items in doclib.Items){
//file object to get the files stored in doclib
file = items.File;
if (file.Exists){
string fileurl = file.Url.ToString();
//get collection of fileversions.
vercol = filecols[fileurl].Versions;
//Add the versions to Archive documents library if not a current version.
foreach (SPFileVersion ver in vercol){
if (!ver.IsCurrentVersion){
verID = verID.Append(ver.ID);
verID = verID.Append(",");
urlofFile = ver.File.Name.Split('.');
//Add version ID of the file version for uniqueness
url = urlofFile[0].ToString() + ver.ID + "." + urlofFile[1].ToString();
//get the versions of the file.
dBytes = ver.OpenBinary();
//Add it to Archive doclibrary
currentweb.AllowUnsafeUpdates = true;
doclib2Folder.Files.Add(url, dBytes);
currentweb.AllowUnsafeUpdates = false;
//Delete the versions from the current document library
if (!string.IsNullOrEmpty(verID.ToString())){
string[] vervalues = verID.ToString().TrimEnd(',').Split(',');
int i = 0;
foreach (string s in vervalues){
currentweb.AllowUnsafeUpdates = true;
i = Convert.ToInt32(s);
currentweb.AllowUnsafeUpdates = false;
}verID.Length = 0;
catch (Exception ex)
{//Handle Exceptions....}

3. Deploy the solution as a feauture to your shareoint solution and activate the feature.

Happy Coding...........

Tuesday, March 9, 2010

Create Modify Views and columns In SharePoint Survey....

Had you ever tried creating a new view in sharepoint survey list ?

You won't find an option to do it by default but still you can do it  for a sharepoint survey.

Find the steps below >>   Create New View
  • Open the new view page of any list in your sharepoint site, and copy the URL to a notepad.
  • Open  survey settings page, and from the URL copy the List ID. (List=%7Bxxxxxxxx%2Dxxxx%2Dxxxx%2Dxxxx%2Dxxxxxxxxxxxx%7D).
  • Replace the list name to the last of the URL with the survey list name.
Copy the modified URL to a new browser window, and you are done, you will be now in the 'Create View' page of your Survey List. Create the view as you wish here..........

Find the steps below >> Create New Column.

  • Same the way you did for survey view, copy the new column page url (/_layouts/fldNew.aspx?ListID)
  • Replace the ListID with the survey list ID and you are done.
Find the steps below >> Modify View.

  • Inorder to perform the modify view of the newly created view as per the steps above, just add your list as a webpart to any sharepoint page.
  • In the modify shared webpart option, select the view which you want to edit in the Selected View and  select the option 'Edit the current view'.

Enjoy Customisation...........