Tags » Csharp

Change SharePoint group permissions from code

Is not especially elegant, just “quick and dirty” :)

public static void ChangePermissions(SPSecurableObject subject, SPPrincipal principal, SPRoleType roleType)
{
    SPRoleDefinition roleDefinition = null;
    SPRoleAssignment roleAssignment = subject.RoleAssignments.GetAssignmentByPrincipal(principal);

    if (roleAssignment != null && subject != null)
    {
        switch (subject.GetType().Name)
        {
            case "SPList":
                if (!((SPList)subject).HasUniqueRoleAssignments)
                    ((SPList)subject).BreakRoleInheritance(true,false);
                roleDefinition = ((SPList)subject).ParentWeb.RoleDefinitions.GetByType(roleType);
                break;
            case "SPWeb":
                if (!((SPWeb)subject).HasUniqueRoleAssignments)
                    ((SPWeb)subject).BreakRoleInheritance(true, false);
                roleDefinition = ((SPWeb)subject).RoleDefinitions.GetByType(roleType);
                break;
            case "SPItem":
                if (!((SPItem)subject).HasUniqueRoleAssignments)
                    ((SPItem)subject).BreakRoleInheritance(true, false);
                roleDefinition = ((SPItem)subject).Fields.List.ParentWeb.RoleDefinitions.GetByType(roleType);
                break;
            case "SPListItem":
                if (!((SPListItem)subject).HasUniqueRoleAssignments)
                    ((SPListItem)subject).BreakRoleInheritance(true, false);
                roleDefinition = ((SPListItem)subject).Fields.List.ParentWeb.RoleDefinitions.GetByType(roleType);
                break;
            default:
                break;
        }

        if (roleDefinition != null)
        {
            roleAssignment.RoleDefinitionBindings.RemoveAll();
            roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
            roleAssignment.Update();
        }
    }
}
… 73 more words
SharePoint

A galaxy var, var away

My current project has me writing C# scripts in Unity. Cool. I haven’t written C# for more than a decade. Today I was reviewing code, and noticed this line: 139 more words

Game Programming

Square digit chains


A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before. 99 more words

Code

Internal or public?

Suppose we have a sealed internal class C with a member M intended to be accessed from throughout the assembly:

internal sealed class C
{
  ??? 418 more words
C#

Video : Getting started with the Automapper in C-Sharp

This video will help you in getting started with the Automapper. Please give feedback in comment to improve further

Video

Keep your code DRY

Other day my Boss was reviewing the code written by me. He had a feedback, keep the code DRY. It was great feedback, and I thought why not to share with you that what does DRY mean? 172 more words

CSharp