Friday, July 10, 2009

Create Custom SPGroup and assign Web and List

Wow! 3rd blog in almost 2 years. Well here it is...
The code below shows how to create a custom SPGroup and assign appropriate permissions on the site and list


using (SPSite site = new SPSite("http://sitename"))

{

SPWeb web = site.AllWebs[0];

//use the web owner group as the owner for this new group

SPGroup groupOwner = web.SiteGroups.GetByID(int.Parse(web.Properties["vti_associateownergroup"]));

string groupName = "My Custom Group";

web.SiteGroups.Add(groupName, groupOwner, null, "Custom SharePoint Group for Demo");

//retrieve the newly added group to set roles for it

SPGroup wcmGroup = web.SiteGroups[groupName];

//get the designer role definition to assign to our custom group

SPRoleDefinition designerRoleDefinition = web.RoleDefinitions["Design"];

//assign designer role to our custom group at the web level

SPRoleAssignment roleAssignment = new SPRoleAssignment(wcmGroup);

roleAssignment.RoleDefinitionBindings.Add(designerRoleDefinition);

web.RoleAssignments.Add(roleAssignment);

wcmGroup.Update();

SPList customList = web.Lists["CustomList"];


if (!masterPageGallery.HasUniqueRoleAssignments)

{

masterPageGallery.BreakRoleInheritance(true);

}

//we have unique role assignments

//assign reader role to our custom group for the list

SPRoleDefinition readerRoleDef = web.RoleDefinitions.GetByType(SPRoleType.Reader);

roleAssignment = new SPRoleAssignment(wcmGroup);

roleAssignment.RoleDefinitionBindings.Add(readerRoleDef);

customList.RoleAssignments.Add(roleAssignment);

customList.Update();

web.Update();

}


Happy Coding!


Friday, December 21, 2007

Access Denied error on enabling Office SharePoint Server Publishing Infrastructure

When trying to enable Office SharePoint Server Publishing Infrastructure on a site created using team site (basically a WSS site), I got an Access Denied error message. I found a very good blog by Vincent Rothwell - Activating Office SharePoint Server Publishing Infrastructure - Access Denied. The steps require couple IIS resets and switching application pools - a daunting task if it's a live production environment.

Upon further investigation I found out that if no site collection on a web application is created with publishing feature, you cannot enable the Office SharePoint Server Publishing Infrastructure on a WSS Site (needs further validation). There is a simple workaround that has allowed us to enable Office SharePoint Server Publishing Infrastructure on our WSS site:

  1. Create a new site collection and use Publishing (Collaboration Portal) template at say: http://<portalurl>/sites/test
  2. Enable the Office SharePoint Server Publishing Infrastructure on your required site collection
  3. Delete the temporary site.

If I find more details on this, I will continue to add my findings.

Wednesday, December 19, 2007

Uncheck "Add as a new version to existing files" checkbox on Upload.aspx in MOSS 2007

Steps listed below require modifying the original MOSS files and may be unsupported. Be sure to backup original and modified files.

I was recently asked by my client to have the Add as a new version to existing files option on the upload pages to be "unchecked" by default. This can come in handy on multiple file upload process. Multiple file upload process doesn't "complain" and silently overwrites or ignores existing files.

Unlike v2/2003, this is very easy to do in MOSS/WSSv3:
  1. Open the file Upload.aspx in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\

  2. Locate following tag:
    <asp:CheckBox id="OverwriteSingle" Checked="false" Text="<%$Resources:wss,upload_document_overwrite_file%>" runat="server" />

  3. Change Checked="true" to Checked="false"

  4. Do the same for the OverwriteMultiple tag:
    <asp:CheckBox id="OverwriteMultiple" Checked="false" Text="<%$Resources:wss,upload_document_overwrite_version%>" runat="server" />

  5. Save the file (no IISRESET needed)