am working with a visual web part and I want to update a web part but I need to check out the page first. how can I get the current page and check it out/in in code behind C#

2 Answers 2


It is quite simple to edit. MSDN has actually a full example for PublishingPage.Checkout. To get the current listitem (page), we can use SPContext.Current.ListItem. After that you have to get an instance of PublishingPage, then invoke the Checkout method

var listItem = SPContext.Current.ListItem;
var publishingPage = PublishingPage.GetPublishingPage(listItem);

//...make any changes

var checkInComment = "your optional comment goes here";
SPFile pageFile = publishingPage.ListItem.File;

Simple, isn't it? In the sample from MSDN there are some important checks like is it a publishing page, is it already checked out. I left them out in this simplified code. Make sure you have them in your production code.

  • I am facing a problem that when updating after checkout ,it fails saying that The file is currently checked out or locked for editing by another user, thought I am checking out then updating ! how can I solve that
    – Hassan
    Jun 16, 2013 at 20:28
  • I need more information. What exactly do you want to change in the webpart? Are there any RunWithElevatedPrivileges? Jun 16, 2013 at 20:31
  • I am removing a web part from the page and edit its filter setting and re added to the page, that is what am trying to do
    – Hassan
    Jun 16, 2013 at 20:36
  • Try to update the Title: publishingPage.Title = newTitle; just to be sure that the webpart operation is the problem. Jun 16, 2013 at 20:39
  • the title worked as perfect, but still the web part don't...although all this code is run as elevate privilege
    – Hassan
    Jun 16, 2013 at 21:05

Looks like you need to know who the page is check out to!

in sharepoint, goto site action -> view all site content.

Now goto "pages" and you should see who the page is checked out to. Make sure its the same user account that your logged in with that has the page checked out with.

You can manualy checkout/checkin the page from that!

as a shortcut to that page you can use:


as an example:


I know its not code but you can see if your using the correct account as you could have that page checked out with another account in sharepoint designer and trying to checkin with visual studios under a different account ;) ! that is just one example where you can get into the same situation as you :)

the issue with Anatoly Mironov code is that the page is already checked out, so you cant checkout a page that is already checked out! and your trying to check in a page that is running under a different account! running the code under privlages means your running under a different account than the one you checked out with!

run with elevated pri runs under the Application Pool identity.


your checking in with that account but you need to check in with the same account with what you checked out with ;)

so you have two options! remove the elevated pri in code to run on the same account that is checked out or do as i stated and manualy checkin the page and run the code under elevated priv!

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.