Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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#

share|improve this question
add comment

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);
publishingPage.CheckOut();

//...make any changes

publishingPage.Update();
var checkInComment = "your optional comment goes here";
publishingPage.CheckIn(checkInComment);
SPFile pageFile = publishingPage.ListItem.File;
pageFile.Publish(checkInComment);
pageFile.Approve(checkInComment);

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.

share|improve this answer
    
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 '13 at 20:28
    
I need more information. What exactly do you want to change in the webpart? Are there any RunWithElevatedPrivileges? –  Anatoly Mironov Jun 16 '13 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 '13 at 20:36
    
Try to update the Title: publishingPage.Title = newTitle; just to be sure that the webpart operation is the problem. –  Anatoly Mironov Jun 16 '13 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 '13 at 21:05
show 1 more comment

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:

/_layouts/viewlsts.aspx

as an example:

http://yourwebsitename.com/_layouts/viewlsts.aspx

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.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

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!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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