Thursday 24 January 2013

Get Specific list item Version in sharepoint 2010

                                                                     
                                                                     
                                                                     
                                             
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace testhome.Layouts1.testhome
{
    public partial class ApplicationPage1 : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            SPList objList = SPContext.Current.Web.Lists["Vtest"];
            SPQuery objQuery = new SPQuery();
            objQuery.Query = "<Where><Gt><FieldRef Name=\"ID\"/><Value Type=\"Counter\">0</Value></Gt></Where>";
            SPListItemCollection objItemColl = objList.GetItems(objQuery);





            foreach (SPListItem objItem in objItemColl)
            {
                SPListItemVersionCollection objVerisionColl = objItem.Versions;
                int counter = 0;
                string strValue2 = null;
                //Above Line gets all versions of the ListItem. If it returns 1, that means nothing is modified. no version //for this item.
                if (objVerisionColl.Count > 1)
                {
                    //Navigate to each version of the ListItem
                    foreach (SPListItemVersion objVersion in objVerisionColl)
                    {
                        int versionID = objVersion.VersionId;

                        //Returns incremental IDs starting from 512,1024,1536... for each version
                        DateTime timeofcreation = objVersion.Created;
                        //VersionLabel - Gets 1.0,2.0,3.0 as default versions for each iteration
                        string strVersionLabel = objVersion.VersionLabel;


                        SPListItem objLstItm = objVersion.ListItem;
                        string strFirstName = Convert.ToString(objLstItm["VC1"]);
                        string test = objLstItm.Versions[1][1].ToString();
                       
                        SPFieldCollection col = objVersion.Fields;
                        int countt = 0;
                        for (int x = 0; x <= objVersion.Fields.Count; x++)
                        {
                            foreach (SPField ff2 in objVersion.Fields)
                            {
                                Console.WriteLine("{0}", ff2.InternalName);
                                countt++;

                                //here it gives me all content type but i only want the changed ones and it's value
                            }
                        }
                        //foreach (SPField ff in objVersion.Fields)
                        //{
                        //  Console.WriteLine("{0}", ff.InternalName);
                        //}

                        //below here i get the value but for specific column but i don't want to write it in code cause may be next time another column is changed and not the one in my code

                        if (counter == 0)
                        {
                            strValue2 = objLstItm.Versions.GetVersionFromLabel(strVersionLabel)["VC1"].ToString() + ",";
                        }
                        else
                        {
                            strValue2 += objLstItm.Versions.GetVersionFromLabel(strVersionLabel)["VC1"].ToString() + ",";
                        }
                        counter++;
                    }
                }
            }
          }
    }
}

1 comment: