0

I have .docx file on sharepoint which should be read only but I do not know how to set this attribute in my sharepoint application in c# code behind. How to set read-only attribute on file in code behind?

1

Try This, Declare a Client context ctx and get your list/library name:

RoleDefinition roleDef =  ctx.Web.RoleDefinitions.GetByType(RoleType.Reader);
RoleDefinitionBindingCollection roleDefBinding = new RoleDefinitionBindingCollection( ctx );
            ctx.ExecuteQuery();
            roleDefBinding.Add( roleDef );
CamlQuery query;
            query = CamlQuery.CreateAllItemsQuery()
  Microsoft.SharePoint.Client.ListItemCollection items = list.GetItems( query );
            ctx.Load( items );
            ctx.ExecuteQuery();
var grp = cpx.Web.SiteGroups.GetByName(groupName);  
            foreach ( Microsoft.SharePoint.Client.ListItem item in items )
            {
                ctx.Load( item, i=>i.RoleAssignments, i=>i.HasUniqueRoleAssignments );
                ctx.ExecuteQuery();
                if ( !item.HasUniqueRoleAssignments )
                {
                    item.BreakRoleInheritance( true, false );
                    ctx.ExecuteQuery();
                }
                item.RoleAssignments.Add( grp, roleDefBinding );
                ctx.ExecuteQuery();
            }
  • It seems to look good but in my application unfortunately it does not work. I have an Error: Cannot add a role assignment with empty role definition binding collection. – ElConrado Jan 20 '16 at 14:00
  • Have you declared a client context "ctx" as i said. See below How I declare it – Ankit Kumar Jan 20 '16 at 14:43
  • ClientContext ctx= new ClientContext("yoursite/sites/AnkitTest/"); var securePassword = new SecureString(); var usernameqw = "ankitk@D.onmicrosoft.com"; var pwdqq = "yourpwd"; foreach (char c in pwdqq) { securePassword.AppendChar(c); } var onlineCredentials = new SharePointOnlineCredentials(usernameqw, securePassword); ctx.Credentials = onlineCredentials; – Ankit Kumar Jan 20 '16 at 14:46
  • Microsoft.SharePoint.Client.GroupCollection grpcol = web.SiteGroups; ctx.Load(grpcol); ctx.ExecuteQuery(); – Ankit Kumar Jan 20 '16 at 14:47
  • I belive that my contex is declared correctly. An error occured on line with: item.RoleAssignments.Add( grp, roleDefBinding ); – ElConrado Jan 21 '16 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.