If we want to unique permissions to SharePoint objects (Lists, document libraries, folders etc..) we use BreakRoleInheritance. It takes a Boolean parameter which is either true or false.
BreakRoleInheritance(true) - means copy all the permissions from the parent and then maintain its own unique permissions for all the future changes on that object.
BreakRoleInheritance(false) - means do not copy any permissions from the parent and maintain its own unique permissions for all the future changes on the object.
For some reason BreakRoleInheritance(false) was not working and was throwing an error. I am not the only person to complain about this, but the same with many developers on the Google. Below is the final code which worked for me.
//Code to fix BreakRoleInheritance(false) issues
//with BreakRoleInheritance(true) permissions are inherited and then removed one by one.
//It is responsible for locking Permissions table for long time and potentially resulting
//into the deadlock
oWeb.AllowUnsafeUpdates = true;
SPFolder oPermissionFolder = oList.RootFolder.SubFolders.Add(FolderName);
oPermissionFolder.Item.BreakRoleInheritance(false);
oFolderItem = oPermissionFolder.Item;
oFolderItem["Title"] = false.ToString() + ";1";
if(!oWeb.AllowUnsafeUpdates)
oWeb.AllowUnsafeUpdates = true;
int raCount = oFolderItem.RoleAssignments.Count;
for (int i = raCount - 1; i >= 0; i--)
{
oFolderItem.RoleAssignments.Remove(i);
}
//oFolderItem.Update();
oFolderItem.SystemUpdate();
Showing posts with label SharePoint 2007 Best Practices. Show all posts
Showing posts with label SharePoint 2007 Best Practices. Show all posts
Thursday, February 9, 2012
Sunday, March 27, 2011
Best Object Model Coding Practices for SharePoint 2007
I was working on performance tuning a SharePoint 2007 application and these are the common bad coding practices which developers make unintentionally.
Never Use SPList.Items.GetItemById(ID):
Refer this link for further details
http://www.3guysonsharepoint.com/?p=197
SPWeb.EnsureUser Vs. SpWeb.AllUsers:
The best way to check if the user exists / have access to the site / web is this way.
SPWeb.AllowUnsafeUpdates = true;
SPUser user = webInUserContext.EnsureUser("ad\\C001234");
SPWeb.AllowUnsafeUpdates = false;
Don't forget to use AllowUnsafeUpdates if the logged in user only has read/contribute permissions, otherwise you will get "Access Denied". It is not necessary to use AllowUnsafeUpdates if you are running code with elevated privileges or impersonation.
http://blog.qumsieh.ca/2008/06/26/spweb-siteusers-vs-spweb-users/
This is not a good way.
SPUser user = webInUserContext.AllUsers["ad\\C001234"];
Windows SharePoint Services 3.0 SDK
SPWeb.AllUsers – Gets the collection of user objects that represents all users who are either members of the site or who have browsed to the site as authenticated members of a domain group in the site.
SPWeb.SiteUsers – Gets the collection of all users that belong to the site collection.
SPWeb.Users – Gets the collection of user objects that are explicitly assigned permissions on the Web site.
SPWeb.EnsureUser - Checks whether the specified login name belongs to a valid user of the website, and if the login name does not already exist, adds it to the website.
Best way of Accessing SharePoint Groups:
I think this is the best way of accessing SharePoint groups if we know the name of the group.
SPGroupCollection groupColl = newWeb.SiteGroups.GetCollection(new String[] { "Readers", "Contributers" });
This one gets SharePoint groups only which you want where as below one gets all the SharePoint Groups
SPGroupCollection groupColl = newWeb.SiteGroups;
Efficient way of adding list item to SharePoint list:
Refer the link below
http://msdn.microsoft.com/en-us/library/bb687949(office.12).aspx
public static class Extensions
{ /*efficient way of adding list item to SharePoint list
* http://msdn.microsoft.com/en-us/library/bb687949(office.12).aspx */
public static SPListItem AddItem(this SPList list)
{
const string EmptyQuery = "0";
SPQuery q = new SPQuery { Query = EmptyQuery };
return list.GetItems(q).Add();
}
}
Usage…
SPListItem item = list.AddItem();
List item query elapsed time:
I got this message in the SharePoint ULS log.
01/21/2011 10:30:02.00 w3wp.exe (0x1320) 0x1944 Windows SharePoint Services Database 8sli Monitorable List item query elapsed time: 4508 milliseconds, Additional data (if available): Query HRESULT: 0 List internal name, flags, and URL: {A134D627-5580-49FD-883C-9D1325CE6CD3}, flags=0x0000000020801088, URL="http://SiteURL/Lists/Tickets/AllItems.aspx" Query XML: "<Query><OrderBy><FieldRef Name="Modified" Ascending="FALSE"/></OrderBy></Query>" SQL Query: " SELECT TOP 101 t1.[Type] AS …….
I was wondering what this message in the SharePoint ULS log means, finally figured out that this message is logged whenever we try to load a SharePoint List View which has large number of rows and which does not use default view sorting order (List Item ID)
I our case our SharePoint List had 1,000,00 rows in it and the AllItems View is being sorted by Last modified descending.
Select Distinct or Unique values from SharePoint List column:
Refer the link below
http://blog.visualstudioteamsystem.com/post.aspx?item=33
if (web.ServerRelativeUrl == "/")
{
ticketsList = (SPList)web.GetList(ticketsListURL);
}
else
{
ticketsList = (SPList)web.GetList(string.Concat(web.ServerRelativeUrl, ticketsListURL));
}
SPField field = ticketsList.Fields.GetFieldByInternalName("CreatedByHistorical");
object[,] values;
uint numberValues = ticketsList.GetDistinctFieldValues(field, out values);
for (int i = 0; i < numberValues; i++)
dropDownListEnterers.Items.Add(values.GetValue(0, i).ToString());
Subscribe to:
Posts (Atom)