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#
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.
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!