Sunday, 20 May 2012

Microsoft.SharePoint.SPQueryThrottledException: The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator


Hi All,

In our application we are using CrossListQueryCache::GetSiteData() to get the data from the another web application and certainly it stopped working. After looking into logs it found the following exceptions:

05/13/2012 14:13:28.05 w3wp.exe (0x5140)                                0x1B54 Web Content Management        Publishing                                8vy9     Monitorable       CrossListQueryCache::GetSiteData() caught exception (Microsoft.SharePoint.SPQueryThrottledException: The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator. ---> System.Runtime.InteropServices.COMException (0x80070024): The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.     at Microsoft.SharePoint.Library.SPRequestInternalClass.CrossListQuery(String bstrUrl, String bstrXmlWebs, String bstrXmlLists, String bstrXmlQuery, ISP2DSafeArrayWriter pCallback, Object& pvarColumns)     at Microsoft.SharePoint.Library.SPRequest.CrossListQuery(String bstrUrl, String bstrXmlWebs, String bstrXmlLists, String bstrXmlQuery, ISP2DSafeArrayWriter pCallback, Object& pvarColumns)     --- End of ...   08dfc04d-71f4-4264-8c80-ebe5c3f2b4ca
05/13/2012 14:13:28.05* w3wp.exe (0x5140)                                0x1B54 Web Content Management        Publishing                                8vy9     Monitorable       ...inner exception stack trace ---     at Microsoft.SharePoint.SPGlobal.HandleThrottleException(COMException comEx)     at Microsoft.SharePoint.Library.SPRequest.CrossListQuery(String bstrUrl, String bstrXmlWebs, String bstrXmlLists, String bstrXmlQuery, ISP2DSafeArrayWriter pCallback, Object& pvarColumns)     at Microsoft.SharePoint.SPWeb.GetSiteData(SPSiteDataQuery query)     at Microsoft.SharePoint.Publishing.CachedArea.GetSiteData(SPWeb web, SPSiteDataQuery siteDataQuery, Boolean useSpQueryOnList)     at Microsoft.SharePoint.Publishing.CrossListQueryCache.GetSiteDataResults(CachedArea cachedArea, SPWeb web, SPSiteDataQuery query, Boolean useSpQueryOnList)) for query:  '<ViewFields><FieldRef Name="Title" /><FieldRef Name="FileLeafRef" /><FieldRef Name="FileRef" /><FieldRef Name="ServerUrl"...    08dfc04d-71f4-4264-8c80-ebe5c3f2b4ca

So to resolve this problem went to 

  1.  Central Admin >> Manage web applications
  2.  Select the web application
  3.  General Settings >> Resource Throttling
  4.  Increased the value of “List View Threshold” from 5000 to 15000
and web part started working and showing the result.

But if gone through the page http://wl2005080:22033/_admin/vsgeneralsettings.aspx?type=res&WebApplicationId=61aa10c6fb754f26a577c21cacd98a24&IsDlg=1 there are different threshold values. If we gone through these different settings those are self-descriptive.   But following are the few points:

1.     Difference between List View Threshold and List View Threshold for Auditors and Administrators is the Threshold value for the query executed by normal user and super user (administrator – having special rights)

2.     List View Threshold for Auditors and Administrators this setting works with Object Model Override setting. The second setting allows administrator to retrieve through object model up to the limit specified in List View Threshold for Auditors and Administrators setting.





 
Thanks!

As usual Suggestions / Comments are most welcome J


Monday, 7 May 2012

Adding SharePoint group to the site programmatically


Hello,

We had a requirement like when one of the sub site gets created, permission inheritance should be broken and new group with specific permission level should be added.

Here I will discuss the steps to break the inheritance and adding custom SharePoint group with custom permission level.  We write one feature and included in our custom site definition for the given sub site. We did our code in FeatureActivated event.
  1.  Allow unsafe update on the web, this can be done as follows

//get a reference to the web
SPWeb web = properties.Feature.Parent as SPWeb;

//get a current value of AllowUnsafeUpdates property
bool allowunsafeupdate = web.AllowUnsafeUpdates;

//Allow the unsafe updates
web.AllowUnsafeUpdates = true;

2     Break the inheritance permission, this can be done by calling BreakRoleInheritance() method as follows

//break the inheritance permission and not copying the role assignment from parent
web.BreakRoleInheritance(false);
  
Reference: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.breakroleinheritance.aspx

The false argument indicates not to copy the roles assignment from parent object.

Now to add the group to current web, there is a property AssociatedGroups which returns list of SPGroup instance and then using Add() method we can add the group. This Add() method requires instance of SPGroup.

So to get the instance of SPGroup which we need to add, we need to go through the each group for root web(or parent web) and compare with which we need to add like as follows

//My custom group name
string myCustomGroupName = “MyCustomGroupName”;

//Getting all associated groups from root web
IList<SPGroup> associatedGroups = web.Site.RootWeb.AssociatedGroups;

//My custom group declaration
SPGroup myCustomGroup = null;

//Looping through all the groups to get my custom group instance
foreach(SPGroup group in associatedGroups){


//Comparing mycustom group name with current group name
if(group.Name.Equals(myCustomGroupName, StringComparison.InvariantCultureIgnoreCase)){
myCustomGroup = group;
break;
   }//if-end           
}//foreach - end

if(myCustomGroup!= null){
       //Adding my custom group to the group collection of web
web.AssociatedGroups.Add(myCustomGroup);
}

Now to assign the custom permission level to my custom group

//Getting my custom role(custom permission level created)
SPRoleDefinition customPermissionRole = web.RoleDefinitions["CustomPermission", web.Site.RootWeb)];

//Creating role assignment object for my group  
SPRoleAssignment customRoleAssignment = new SPRoleAssignment(myCustomGroup);

//Adding custom permission level to the roledefinitions bindings
customRoleAssignment.RoleDefinitionBindings.Add(customPermissionRole);

web.RoleAssignments.Add(customRoleAssignment);

3.      Update the web and reset the AllowUnsafeUpdates property

web.Update();
web.AllowUnsafeUpdates = allowunsafeupdate;

Any comments / suggestions are welcome J
Hope this will help you! Enjoy reading!

Thanks!