1

I am creating .docx file using template of SharePoint Library and uploading it to the library. But I am facing an error while CheckOut(). Below is my code.

public void UpdateAndCreateFile(SPWeb web)
{
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            string TemplateUrl = string.Empty;
            try
            {
                TemplateUrl = "template path";
            }
            catch { }


            web.AllowUnsafeUpdates = true;
            SPList olist = web.Lists["Document Library"];
            String url = olist.RootFolder.ServerRelativeUrl.ToString();


            string foldername = Convert.ToString("Foldername in document library");
            SPFolder newfolder = web.GetFolder(url + "/" + foldername);

            if (!newfolder.Exists)
            {
                SPFolderCollection folders = web.GetFolder(url).SubFolders;
                //Create new folder
                folders.Add(foldername);
            }

            SPFile file = null;

            file = web.GetFile("Your Document templated full path");

            if (file != null)
            {
                web.AllowUnsafeUpdates = true;
                Stream readStream = file.OpenBinaryStream(); //file is SPFile type
                SPFile uploadedFile = newfolder.Files.Add(newfolder.Url + @"/" + "NewDocName.docx", readStream, true);
                uploadedFile.CheckOut();
                SPListItem listitem = uploadedFile.Item;
                // Details is mapped in document
                listitem["Details"] = "this content will add in document";
                listitem.Update();
                uploadedFile.Update();
                uploadedFile.CheckIn(string.Empty);
                web.AllowUnsafeUpdates = false;
            }
        });
    }
    catch (Exception ex)
    {
        // handle exception here
    }

}

Where I am going wrong.

Any help would be appreciated.

Thanks

  • Remove the first web.AllowUnsafeUpdates = true;. On topic: you don't need uploadedFile.Update(); – Paul Strupeikis Mar 9 '16 at 16:30
  • thanks. but i am getting error in checkout() and am directly thrown to catch block – DRS Mar 9 '16 at 16:33
1

Try to get a new instance of the SPFile object right before you try the .CheckOut() instruction. I may be wrong, but I think you are experiencing a pretty common transaction related issue.

if (file != null)
        {
            web.AllowUnsafeUpdates = true;
            Stream readStream = file.OpenBinaryStream(); //file is SPFile type
            SPFile uploadedFile = newfolder.Files.Add(newfolder.Url + @"/" + "NewDocName.docx", readStream, true);

            var fileUrl = SPUtility.GetFullUrl(web, uploadedFile.Url);
            uploadedFile = web.GetFile(fileUrl);
            // also check with 'uploadedFile=uploadedFile.Item.File;', 
            // I think it may work even this way.

            uploadedFile.CheckOut();
            SPListItem listitem = uploadedFile.Item;
            // Details is mapped in document
            listitem["Details"] = "this content will add in document";
            listitem.Update();
            uploadedFile.Update();
            uploadedFile.CheckIn(string.Empty);
            web.AllowUnsafeUpdates = false;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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