1

Im trying to remove all content in page. but i get an exception when i trying to do this, it says that the file is not checked out and i have to do this. And wehn i use file.CheckOut() i see that the file is checked out to the systemAcount and i get exception that the file is CheckedOut to another user

How to do that i can ignore the outcheck, or CheckOut to the current/specific user

        var webMngr = web.GetLimitedWebPartManager("Sidor/default.aspx", PersonalizationScope.Shared);
        List<System.Web.UI.WebControls.WebParts.WebPart> webPartList = (from System.Web.UI.WebControls.WebParts.WebPart webPart in webMngr.WebParts

                                                                        select webPart).ToList();
        SPFile file = web.GetFile(web.Url+"Sidor/default.aspx");
        //file.CheckOut();
        web.AllowUnsafeUpdates = true;


        object _lock = new object();

        lock (_lock)
        {
            for (int i = 0; i < webMngr.WebParts.Count; i++)
            {
                webMngr.DeleteWebPart(webPartList[i]);
            }
        }

2 Answers 2

1

You should elevate the permission and checkin the file, then checkout and delete.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    var webMngr = web.GetLimitedWebPartManager("Sidor/default.aspx", PersonalizationScope.Shared);
    List<System.Web.UI.WebControls.WebParts.WebPart> webPartList = (from System.Web.UI.WebControls.WebParts.WebPart webPart in webMngr.WebParts select webPart).ToList();
    SPFile file = web.GetFile(web.Url+"Sidor/default.aspx");

    web.AllowUnsafeUpdates = true;
    if (file.CheckOutType == SPFile.SPCheckOutType.Online)
    {
        Console.WriteLine(file.Name);
        file.CheckIn("Comment");
    }  
    file.CheckOut();

    object _lock = new object();

    lock (_lock)
    {
        for (int i = 0; i < webMngr.WebParts.Count; i++)
        {
            webMngr.DeleteWebPart(webPartList[i]);
        }
    }
    web.AllowUnsafeUpdates = false;
});
5
  • didnt hepl, Got the same exception "the file is ouchecked to another user" Im logged in to the windows as Admin, and when i checkout the file i se that the file is checkedout to the FarmAdmin/SystemAcount.
    – gästa
    Nov 16, 2014 at 1:11
  • Can you remove the lock and check? Nov 16, 2014 at 1:15
  • Still the same exception
    – gästa
    Nov 16, 2014 at 1:33
  • From where this code is getting executed, webpart or console application or event receiver? Nov 16, 2014 at 1:38
  • im running this from webpart on sharepoint
    – gästa
    Nov 16, 2014 at 10:11
0

If you have administrative rights, it is possible to override the check-out via the View All Site Content page:

  1. Site Actions
  2. View All Site Content
  3. Pages
  4. Hover the item you want to check out, and from the context-menu (arrow-down next to the filename), choose "Discard Check Out"

It is also possible to perform this routine through SharePoint Designer.

Another option could be to use UndoCheckOut:

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfile.undocheckout.aspx

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.