I have a custom webpart. Inside this webpart there is some logic to update the webpart settings. See below the code.

I edit the page where the webpart is located. I click on a button which will update the webpart and run the code below.

It gives me an exception like:

WebPartPageUserException: The file is not checked out. You must first check out this document before making changes.

Why do I got this exception? I checked this page out.

private bool SetWebPartProperties(string pageUrl, string webPartId)
            SPWeb currentWeb = SPContext.Current.Web;

            PublishingWeb publishingWeb = null;
            PublishingPage page = null;

                    using (SPSite site = new SPSite(currentWeb.Url))
                        using (SPWeb web = site.OpenWeb())
                            if (PublishingWeb.IsPublishingWeb(web))
                                publishingWeb = PublishingWeb.GetPublishingWeb(web);
                                PublishingPageCollection pages = publishingWeb.GetPublishingPages();

                                if (pages[pageUrl] != null)
                                    page = pages[pageUrl];
                                    if (page.ListItem.File.CheckOutType == SPFile.SPCheckOutType.None)

                            SPLimitedWebPartManager wpm = null;

                            // Here you will use the url of the page to get an instance of its
                            // WebPartManager - with this object, you will be able to find and
                            // update your web part....
                            wpm = web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared);

                            // ... using its id to find it in the WebParts collection of the
                            // WebPartManager.
                            System.Web.UI.WebControls.WebParts.WebPart wp = wpm.WebParts[webPartId];

                            // ... and this setting is important, otherwise it won't accept
                            // changes by code. Notice that I'm setting it back to false after
                            // the update...
                            wpm.Web.AllowUnsafeUpdates = true;

                            // DO SOME LOGIC

                            // ... and then you save it...
                            wpm.SaveChanges(wp); // HERE THE ERROR OCCURED
                            wpm.Web.AllowUnsafeUpdates = false;


                            string checkinComment = string.Format("Hoofditems: {0} Aantal nieuwsitems: {1}", "", maxDays);
                            SPFile pageFile = page.ListItem.File;

            return true;

1 Answer 1


The problem may be that you use an elevated section, meaning the code runs under the identity of System. However, the page is being edited by (and checked-out to) the current user, the one who clicked the button.

Thus, the error probably means the code cannot edit a file checked-out by someone else (and you never reach you page.CheckOut()call by the way).

Why do you need that elevation of privileges? As the current user has entered edit mode, he has the permissions to edit the Web part... so you may use the contextual SPWeb directly (SPContext.Current.Web).

  • you are right. I think it is not working only when you are debugging. On production it works fine with the elevation privileges.
    – Ola
    Commented Apr 17, 2015 at 14:26
  • In all cases, you probably don't need that elevation of privileges. As a general rule, avoid elevations when not necessary.
    – Evariste
    Commented Apr 17, 2015 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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