Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

SharePoint - Create Custom Permission Level and List All available Permission

Article:
Viewed:  1803 
Posted On:  26/08/2013 21:18:08 
How we can create new custom permission level in SharePoint.. How we can list all available permission... How we can show the details of any permission... 

In this article I am going to show programmatically how we can list all available permission in a SharePoint site and how we can show the detail of any permission ie. name, description, base permission etc..

I am also showing how we can create our own custom permission in SharePoint programmatically.

For this I created Blank SharePoint Solution and mapped it with our SharePoint site. I added 2 pages here..

1. GetAllPermission.aspx (To show all permission)

2. AddNewPermission (To add a custom permission).

I deployed the solution and my functionality is:

To Add a new custom Permission...

AddPermission.png

Image 1.

After Creating New Permission you can check in SharePoint Site:

Permission.png

Image 2.

AddNewPermission.aspx is:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>

<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"

    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddNewPermission.aspx.cs"

    Inherits="SP_Permission.Layouts.SP_Permission.AddNewPermission" DynamicMasterPageFile="~masterurl/default.master" %>

 

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

    <table>

        <tr>

            <td height="10px">

            </td>

        </tr>

    </table>

    <table cellpadding="5" cellspacing="5" width="70%" align="center" style="border: 2px solid Green;">

        <tr>

            <td style="background-color: #@F5F5F5; width: 100%; font-weight: bold; font-size: 12pt;"

                colspan="4">

                Add New Permission:

            </td>

        </tr>

        <tr>

            <td width="25%" align="right">

                Permission Name:

            </td>

            <td align="left">

                <asp:TextBox ID="txtName" runat="server" Width="300px"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td width="25%" align="right">

                Permission Description:

            </td>

            <td align="left">

                <asp:TextBox ID="txtDesc" runat="server" Width="300px" Height="70px" TextMode="MultiLine"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td colspan="4" align="center">

                <asp:Button ID="btnAdd" runat="server" OnClick="btnAddPermission_Click" Text="Add Permission" />

            </td>

        </tr>

    </table>

</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">

    Add New Permission Level

</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"

    runat="server">

    Add New Permission Level

</asp:Content>

 
AddNewPermission.aspx.cs is:

using System;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

 

namespace SP_Permission.Layouts.SP_Permission

{

    public partial class AddNewPermission : LayoutsPageBase

    {

        string mySite = "http://localhost:7000";

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void btnAddPermission_Click(object sender, EventArgs e)

        {

            using (SPSite site = new SPSite(mySite))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPRoleDefinition roleDefinition = new SPRoleDefinition();

                    roleDefinition.Name = txtName.Text;

                    roleDefinition.Description = txtDesc.Text;

                    roleDefinition.BasePermissions = SPBasePermissions.AddAndCustomizePages | SPBasePermissions.ApplyStyleSheets |

                                                     SPBasePermissions.CreateGroups| SPBasePermissions.OpenItems |

                                                     SPBasePermissions.EditListItems | SPBasePermissions.ViewListItems |

                                                     SPBasePermissions.ViewPages | SPBasePermissions.Open |

                                                     SPBasePermissions.ViewFormPages;

                    web.RoleDefinitions.Add(roleDefinition);

                }

            }

 

            Response.Redirect("GetAllAvaliablePermission.aspx");

        }

    }

}

 

ToListAllPermission (GetAllAvaliablePermission.aspx)

ListAllPermission.png

Image 3.

Select any permission and click on GetDetail..

PermissionDetail.png

Image 4.

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>

<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"

    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetAllAvaliablePermission.aspx.cs"

    Inherits="SP_Permission.Layouts.SP_Permission.GetAllAvaliablePermission" DynamicMasterPageFile="~masterurl/default.master" %>

 

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

    <table>

        <tr>

            <td height="10px">

            </td>

        </tr>

    </table>

    <table cellpadding="5" cellspacing="5" width="70%" align="center" style="border: 2px solid Green;">

        <tr>

            <td style="background-color: #@F5F5F5; width: 100%; font-weight: bold; font-size: 12pt;"

                colspan="4">

                All Avaliable Permission:

            </td>

        </tr>

        <tr>

            <td>

                <asp:ListBox ID="lstAllAvaliableRoles" runat="server" Width="250px" BackColor="#f5f5f5"

                    Height="200px"></asp:ListBox>

            </td>

        </tr>

        <tr>

            <td>

            </td>

        </tr>

        <tr>

            <td colspan="4" align="center">

                <span style="font-family: Verdana; font-size: 8pt; background-color: #F5F5F5;">(Select a permission

                    to get details)</span><br />

                <asp:Button ID="btnGetdetail" runat="server" OnClick="btnGetDetailPermission_Click"

                    Text="Get Detail" />

            </td>

        </tr>

    </table>

    <table>

        <tr>

            <td height="10px">

            </td>

        </tr>

    </table>

    <table cellpadding="5" cellspacing="5" width="70%" align="center" style="border: 2px solid Green;"

        id="permissionDetails" runat="server" visible="false">

        <tr>

            <td style="background-color: #F5F5F5; width: 100%; font-weight: bold; font-size: 12pt;"

                colspan="4">

                Selected Permission Detail:

            </td>

        </tr>

        <tr>

            <td width="25%" align="right">

                Permission Name:

            </td>

            <td align="left">

                <asp:TextBox ID="txtName" runat="server" Width="300px"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td width="25%" align="right">

                Permission Description:

            </td>

            <td align="left">

                <asp:TextBox ID="txtDesc" runat="server" Width="300px" Height="70px" TextMode="MultiLine"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td width="25%" align="right">

                Base Permission Details:

            </td>

            <td align="left">

                <asp:TextBox ID="txtBasePer" runat="server" Width="300px" Height="70px" TextMode="MultiLine"></asp:TextBox>

            </td>

        </tr>

    </table>

</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">

    SP Permission

</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"

    runat="server">

    SP Permission

</asp:Content>

 

 GetAllAvaliablePermission.aspx.cs

using System;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

 

namespace SP_Permission.Layouts.SP_Permission

{

    public partial class GetAllAvaliablePermission : LayoutsPageBase

    {

        string mySite = "http://localhost:7000";

        string strValue = string.Empty;

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!Page.IsPostBack)

            {

                permissionDetails.Visible = false;

                BindAllPermission();

            }

        }

 

        protected void BindAllPermission()

        {

            try

            {

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    using (SPSite site = new SPSite(mySite))

                    {

                        using (SPWeb web = site.OpenWeb())

                        {

                            string spreturnUser = string.Empty;

                            try

                            {

                                int i = 0;

                                foreach (SPRoleDefinition role in web.RoleDefinitions)

                                {

                                    i++;

                                    lstAllAvaliableRoles.Items.Add(i + ": " + role.Name);

                                }

                            }

                            catch (Exception)

                            {

 

                            }

                        }

                    }

                });

            }

            catch (Exception ex)

            {

            }

        }

 

        protected void btnGetDetailPermission_Click(object sender, EventArgs e)

        {

            permissionDetails.Visible = true;

 

            using (SPSite site = new SPSite(mySite))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPRoleDefinition role = web.RoleDefinitions[lstAllAvaliableRoles.SelectedValue.ToString().Split(':')[1].ToString().Trim()];

                    txtName.Text = role.Name;

                    txtDesc.Text = role.Description;

                    SPBasePermissions spPer = role.BasePermissions;

                    txtBasePer.Text = spPer.ToString();

                }

            }

        }

    }

}

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us