Tuesday, June 26, 2012

How to create SharePoint User Group and Assign Permissions


There are some cases where you want to create a group pro grammatically and want to assign permission. e.g. on the Feature receiver (Feature Activated) code.

In this post i am going to explain you how to create group and to assign permissions to this group.
First get the collection of site groups.

SPGroupCollection groups = web.SiteGroups;

web.BreakRoleInheritance(false);
Add the group to this collection. I have created "Test Group" in this example.

groups.Add("Test Group", web.CurrentUser, null, "");
SPGroup grp= web.SiteGroups["Test Group"];          

AllowMembersViewMembership property of this group to false.

This  Gets or sets a Boolean value that specifies whether only group members are    allowed to view the list of members in the group.
grp .OnlyAllowMembersViewMembership = false;
        grp .Update();

Now create a Role Assignment object of this group.

SPRoleAssignment asgn = new SPRoleAssignment(web.SiteGroups[ "Test Group"]);

Create a Role Definition, Here i have used the "Administrator" role, other available roles are

1) None
2) Guest
3) Reader
4) Contributor
5) WebDesigner
6) Administrator  

            SPRoleDefinition role = web.RoleDefinitions.GetByType(SPRoleType.Administrator);

Now assign the Role to the Role Assignment and add the  Role Assignment to the web and update the web

            asgn.RoleDefinitionBindings.Add(role);
            web.RoleAssignments.Add(asgn);
            web.Update();

Now deploy the solution and check the group and its permissions.

I have used this code on the feature activated code that creates group and assign permissions to this.

Tuesday, June 19, 2012

Create SharePoint Lookup Field Programmatically


Lets see how to create a lookup field in a list programmatically

Lets you have a list named "Expense" and another list "Expense Category"

In the "Expense" list you want a lookup field from "Expense Category" list on "Expense Category" field

First add a lookup field named "Category" in the "Expense" list as

   SPList Expense = web.Lists.TryGetList("Expense");
   SPList ExpenseCategory = web.Lists.TryGetList("Expense Category");

   SPFieldCollection _fields = Expense.Fields;

   _fields.AddLookup("Category", ExpenseCategory.ID, true);

In above code i get the collection of fields from the "Expense" list and added a lookup field named "Category" and passed the

GUID of "Expense Category" list from lookup is to be set and set required to true.

Now get the recently created lookup field as

   SPFieldLookup Category = Expense.Fields["Category"] as SPFieldLookup;

Set the lookup field from the "Expense Category" list, Field "Expense Category" and update the field

   Category.LookupField = ExpenseCategory.Fields["Expense Category"].InternalName;


   Category.Update();


deploy the solution and verify the lookup field created in the list

Friday, June 15, 2012

Programmaticlly Create Choice Field with Default Value in SharePoint


In this post i am explaining how to create a choice field in the list and set its default value pro-grammatically

First create a string collection of choice values that you want to use

  StringCollection _statusTypes = new StringCollection();
            _statusTypes.Add("Complete");
            _statusTypes.Add("Incomplete");

Get the field collection from the list and add new choice field to this


 SPList list = web.Lists[listname];
            SPFieldCollection _fields = list.Fields;


  _fields.Add("MyChoiceField", SPFieldType.Choice, true, false, _statusTypes);

Here "MyChoiceField" is the name of field with _statusTypes choice values.

Next to set the default value of the choice field use the code below

Get the choice field
        SPFieldChoice fieldChoice = (SPFieldChoice)list.Fields["MyChoiceField"];
Set its default value              
      fieldChoice.DefaultValue = "Incomplete";
Set the format dropdown or radio button            
      fieldChoice.EditFormat = SPChoiceFormatType.RadioButtons;
and update the field
      fieldChoice.Update();
Now deploy the solution and add new item in the list you will see the default value set for this choice field