Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Item Permissions

Folders work:

I now know how to set the permissions of a folder in a library:

public void ChangeItemPermissions()
{
    _SharePoint.ClientContext _ClientContext = new _SharePoint.ClientContext("https://sharepoint.oshirowanen.com/sites/oshirodev/");
    _ClientContext.Credentials = new NetworkCredential("user", "pass", "oshirowanen.com");

    _SharePoint.Principal user = _ClientContext.Web.EnsureUser(@"oshirowanen\tom");

    var _List = _ClientContext.Web.Lists.GetByTitle("Library1");
    var _Item = _List.LoadItemByUrl("/sites/oshirodev/Library1/Folder1");
    var roleDefinition = _ClientContext.Site.RootWeb.RoleDefinitions.GetByType(_SharePoint.RoleType.Reader);
    var roleBindings = new _SharePoint.RoleDefinitionBindingCollection(_ClientContext) { roleDefinition };
    _Item.BreakRoleInheritance(false,true);
    _Item.RoleAssignments.Add(user, roleBindings);

    _ClientContext.ExecuteQuery();
}

File attempt:

I've tried adding the file name to this line:

var _Item = _List.LoadItemByUrl("/sites/oshirodev/Library1/Folder1/File1.docx");

Notice the (/File1.docx) added to the end of the above line.


Error received:

But that just gives an error:

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=ItemPermissions
  StackTrace:
       at ItemPermissions.Form1.ChangeItemPermissions() in c:\Users\Oshirowanen\Documents\Visual Studio 2013\Projects\ItemPermissions\ItemPermissions\Form1.cs:line 46
       at ItemPermissions.Form1.button1_Click(Object sender, EventArgs e) in c:\Users\Oshirowanen\Documents\Visual Studio 2013\Projects\ItemPermissions\ItemPermissions\Form1.cs:line 345
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at ItemPermissions.Program.Main() in c:\Users\Oshirowanen\Documents\Visual Studio 2013\Projects\ItemPermissions\ItemPermissions\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Relevant info:

This is a WinForm app running on a local machine, created with C#, using .NET 4.0. SharePoint version is 2010.


The question:

How do I set the permissions for a specific file? As I already know how to set permissions for a specific folder.

like image 546
oshirowanen Avatar asked May 23 '17 15:05

oshirowanen


People also ask

How do I change the item-level permissions on a workflow?

Click on the workflow, select the workflow then you will see. Select Start workflow automatically when an item is created. and start workflow automatically when an item is changed. Now go to the permissions of the item and verify that the permission workflow go executed.

How do I set permissions on a SharePoint list?

On the permissions page for the list, on the Edit tab, click Grant Permissions. Type the name of the group or the individual you want to grant access to in the Users/Groups box. Choose the level of permissions you want the group or individuals to have. Click OK.

How do I give item permission in SharePoint 2013?

To do this, navigate to Custom List -> List Settings -> Advanced Settings -> Item-level permissions.


1 Answers

I reviewed the 2010 CSOM documentation, and the method you are using LoadItemByUrl doesn't exist. Is that an extension you wrote, perhaps this one? If so then the problem must be in that extension, probably in the CAML query.

As a reference, this 2010 CSOM code worked ok against a SharePoint 2010 lab. Note that it uses GetItemByID:

        ClientContext _ClientContext = new ClientContext("http://team/sites/Team1/");
        _ClientContext.Credentials = new NetworkCredential("administrator", "*****", "corp");
        Principal user = _ClientContext.Web.EnsureUser(@"corp\administrator");
        var _List = _ClientContext.Web.Lists.GetByTitle("Shared Documents");
        var _Item = _List.GetItemById(23);
        var roleDefinition = _ClientContext.Site.RootWeb.RoleDefinitions.GetByType(RoleType.Reader);
        var roleBindings = new RoleDefinitionBindingCollection(_ClientContext) { roleDefinition };
        _Item.BreakRoleInheritance(false, true);
        _Item.RoleAssignments.Add(user, roleBindings);
        _ClientContext.ExecuteQuery();
like image 157
andresm53 Avatar answered Sep 21 '22 21:09

andresm53