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++;
                    }
                }
            }
          }
    }
}

Working with Large Lists in SharePoint 2010 - List Throttling?


ms, and someone created a view that would return all of the items in the list in a single page.  List throttling ensures that such a request would not be allowed to execute.  The hit on the server is alleviated, and the user gets a nice little message that says sorry, we can’t retrieve all of the data you requested because it exceeds the throttle limit for this list.
The kinds of operations that can trigger hitting this limit though aren’t limited to viewing data – that’s just the easiest example to demonstrate.  There are other actions that can impact a large number of rows whose execution would fall into the list throttle limits.  For example, suppose you had a list with 6000 items and a throttle limit of 5000.  You create a view that only displays 50 items at a time, but it does a sort on a non-indexed column.  Behind the scenes, this means that we need to sort all 6000 items and then fetch the first 50.  If you are going to delete a web with large flat lists you potentially have the same problem.  We need to select all of the items for all of the lists as part of the site deletion, so we could again hit the throttling limit.  These are just a few examples but you can start to imagine some of the others.
So how does this work and how do we manage it?  It all starts in central admin.  List throttling is an attribute that you will generally manage at the web application level.  So if you go into Central Admin, click on Application Management, then click on Manage Web Applications.  Click a single web application to select it, then in the ribbon click on the General Settings drop down and select the Resource Throttling menu item.  It displays a dialog with the several options; I’ll only cover the ones related to list item throttling in this blog:
·        List View Threshold – this is the maximum number of items that can be retrieved in one request.  The default value is 5,000.  Important to note as well, the smallest you make this value is 2,000.
·        Object Model Override – as described above, this option needs to be enabled in order to enable super users to retrieve items through the object model, up to the amount defined in the List query size threshold for auditors and administrators.
·        List View Threshold for Auditors and Administrators – this is a special limit for “super users”.  It is important to understand that this DOES NOT allow these super users to see more items in a list view.  This property is used in conjunction with the Allow object model override property described below.  That means that if the Allow object model override property is set to Yes, then these super users can retrieve up to the number of items set in this property, but only via the object model.  The way you become a “super user” is a farm admin creates a web application policy for you that grants you rights to a permission level that includes either the Site Collection Administrator and/or Site Collection Auditor rights.  By default both the Full Read and Full Control permission levels include these rights, and you can create your own custom policy permission levels that do as well.  Creating this policy is done the same way as it was in SharePoint 2007.
·        List View Lookup Threshold – again, nothing to do with the maximum number of rows returned from a list but it’s right in the middle of these so I couldn’t leave it out.  This one is self-explanatory I think.
·        Daily Time Window for Large Queries – this option allows you to create a block of time during the day, typically when usage is low, that you will allow queries to run and not enforce the list throttling limits. The one thing to remember here is that if you execute a query during that time period, it will run until complete.  So if it’s still running when the daily window period closes, the query will continue to execute until all results have been returned.
There are a couple of additional exceptions to the information above:
1.       If you are box administrator on the WFE where the data is being requested, and you have at least Read rights to the list data, then you will see ALL the rows.  That means if you have 10,000 rows in a list and you execute a query or view that has no filters, you will get back all 10,000 rows.
2.       In the object model a list (and library) is represented by the SPList class.  It has a new property in SharePoint 2010 called EnableThrottling.  On a list by list basis you can set this property to false.  When you do that, throttling will not be enabled for views or object model queries.  So again, if your list has 10,000 items and you execute a query or view that has no filters, you will get back all 10,000 rows.
In order to retrieve information using the object model in order to retrieve up to the number of items specified in the List query size threshold for auditors and administrators property, there is a property you need to set in your query object.  The property is called QueryThrottleMode and it applies to the SPQuery and SPSiteDataQuery classes.  You simply set this property to Override and then use your class instance to query.  Here’s a simplified example:
using (SPSite theSite = new SPSite("http://foo"))
{
using (SPWeb theWeb = theSite.RootWeb)
{
SPList theList = theWeb.Lists["My List Name"];

SPQuery qry = new SPQuery();
qry.QueryThrottleMode = SPQueryThrottleOption.Override;

//set the Query property as needed to retrieve your data

            SPListItemCollection coll = theList.GetItems(qry);

            //do something with the data
}
}