Paul Liebrand’s Weblog

Welcome to my blog mainly about SharePoint

You must first check out this document before making changes and RoleAssignments

This morning I ran into an issue with SharePoint where I was attempting to adjust the permissions of a list item and I got the following error message:

Microsoft.SharePoint.SPException: The file “http://portal.company.com/sites/acme/Memos/Q1 Memo.docx” is not checked out.  You must first check out this document before making changes. —> System.Runtime.InteropServices.COMException (0x8007009E): The file “http://portal.company.com/sites/acme/Memos/Q1 Memo.docx” is not checked out.  You must first check out this document before making changes.

Here is a code sample I was using:

   1: using (SPSite site = new SPSite("http://portal.company.com"))
   2: {
   3: 
   4:     using (SPWeb web = site.OpenWeb())
   5:     {
   6:         web.AllowUnsafeUpdates = true;
   7: 
   8:         SPRoleDefinition definition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
   9: 
  10:         SPList list = web.Lists["Announcements"];
  11:         if (list.Items.Count > 0)
  12:         {
  13:             SPListItem listItem = list.Items[0];
  14: 
  15:             SPRoleAssignment assignment = new SPRoleAssignment(web.SiteUsers[@"domain\user"]);
  16:             assignment.RoleDefinitionBindings.Add(definition);
  17: 
  18:             if (!listItem.HasUniqueRoleAssignments)
  19:                 listItem.BreakRoleInheritance(false); // true if you want to copy permissions
  20: 
  21:             listItem.RoleAssignments.Add(assignment);
  22:             listItem.Update();
  23:         }
  24: 
  25:     }
  26: }

When changing the role assignments via the SharePoint interface everything worked as expected.  After further investigation, I found the problem was with line 22 (above). If you are just changing the role assignments for an item, list, or web, you do not need to call the Update() method. The MSDN description for the Update method states:

Updates the database with changes made to the list item

I guess a role assignment does not count as a change to the item. Of course, if you were changing metadata, or other properties in your code, you would be checking out the item and you would not run into this problem.

I could not find any clear and concise documentation that specifies when you need to call the Update method and when you do not have too.  If anyone knows, please let me know.

Thank you!

Advertisements

March 18, 2008 Posted by | SharePoint | , | 1 Comment