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

I am trying to add a set of documents and folders through webservice.While creating the documents when two users try to access the document library with diffrent files and folders i am getting this save conflict error.Note:At no instant will 2 users be able to access the same file or folders as it is handled ny a lock in the calling application.

But when multiple users try to add documents to the library this error occurs.

Any help in ny form is appreciated.I am unable to identify the problem statement here !! :( The code snippet is as below:

    #region Create Folder structures in Sharepoint
    private void Create_Folders(string LoanID, string PackName, string PackNo, string customerName)
    {
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                //Get sharepoint url and document library name from Constants file
                string SharepointUrl = ConfigValues.SharePointRootUrl;
                // string DocLib = Constants.SharepointDoclib;
                string DocLib = ConfigValues.DocumentLibraryName;

                SPSite _MySite = new SPSite(SharepointUrl);
                SPWeb _MyWeb = _MySite.OpenWeb();
                SPDocumentLibrary _MyDocLibrary = (SPDocumentLibrary)_MyWeb.Lists[DocLib];
                SPFolder folder = _MyWeb.GetFolder(SharepointUrl + DocLib + "/" + LoanID + "/" + PackName + "-" + PackNo);
                SPFolderCollection _MyFolders = _MyWeb.Folders;

                //Check if folder with the same loan id already exists within the document library
                bool sflag = FolderExists(SharepointUrl + DocLib + "/" + LoanID, _MyWeb);

                //Only if the folder does ot exist ,a new folder would be created,
                //else the existing folder would be verified to check if a folder with the same pack name already exists.
                if (!sflag)
                {
                    _MyWeb.AllowUnsafeUpdates = true;
                    _MySite.AllowUnsafeUpdates = true;

                    _MyFolders.Add(SharepointUrl + DocLib + "/" + LoanID);

                    _MyDocLibrary.Update();
                    _MyWeb.Update();

                    _MyWeb.AllowUnsafeUpdates = false;
                    _MySite.AllowUnsafeUpdates = false;

                    if (ConfigValues.LogTraceFlag == "true")
                    {
                        Diagnostics.WriteToTopUpsDiagnosticsLog(Constants.CreateInfoLog, "Create_Folders()", LoanID, "Loan Folder Created successsfully");
                    }
                }

                else
                {
                    if (ConfigValues.LogTraceFlag == "true")
                    {
                        Diagnostics.WriteToTopUpsDiagnosticsLog(Constants.CreateInfoLog, "Create_Folders()", LoanID, "Loan Folder already exists");
                    }
                }

                //Creation of Pack Folders

                _MyWeb.AllowUnsafeUpdates = true;
                _MySite.AllowUnsafeUpdates = true;

                //Set properties for Loan Folder
                SPFolder loanfolder = _MyWeb.GetFolder(SharepointUrl + DocLib + "/" + LoanID);

                loanfolder.Item["InvolvedParty"] = customerName;
                loanfolder.Item.Update();
                if (ConfigValues.LogTraceFlag == "true")
                {
                    Diagnostics.WriteToTopUpsDiagnosticsLog(Constants.CreateInfoLog, "Create_Folders()", LoanID, "Updated customer name to the loan folder");
                }
                _MyDocLibrary.Update();
                _MyWeb.Update();

                //If a folder with the same name already exists,delete the exisitng folder
                if (folder.Exists)
                {
                    SPFileCollection fileCollection = folder.Files;

                    //Get the list of files inside the existing pack folder and verify if any of the files 
                    //are checked out or locked by any user.If the file is checked out,undocheckout is given and
                    //then the entire Pack Folder is deleted

                    for (int fileIndex = 0; fileIndex < fileCollection.Count; fileIndex++)
                    {
                        if (fileCollection[fileIndex].CheckedOutBy != null)
                        {
                            fileCollection[fileIndex].UndoCheckOut();
                        }
                    }

                    _MyDocLibrary.Update();
                    _MyWeb.Update();

                    //Delete Pack Folder
                    folder.Delete();

                    _MyDocLibrary.Update();
                    _MyWeb.Update();

                    if (ConfigValues.LogTraceFlag == "true")
                    {
                        Diagnostics.WriteToTopUpsDiagnosticsLog(Constants.CreateInfoLog, "Create_Folders()", LoanID, "Pack folder with the same name already exists.Hence deleted");
                    }
                }

                //Now add a folder with the same pack name
                _MyFolders.Add(SharepointUrl + DocLib + "/" + LoanID + "/" + PackName + "-" + PackNo);

                if (ConfigValues.LogTraceFlag == "true")
                {
                    Diagnostics.WriteToTopUpsDiagnosticsLog(Constants.CreateInfoLog, "Create_Folders()", LoanID, "Pack folder created succesfully" + PackName + "-" + PackNo);
                }

                _MyDocLibrary.Update();
                _MyWeb.Update();

                _MyWeb.AllowUnsafeUpdates = false;
                _MySite.AllowUnsafeUpdates = false;

                //Perform Cleanup
                _MyWeb.Dispose();
                _MySite.Dispose();

                if (ConfigValues.LogTraceFlag == "true")
                {
                    Diagnostics.WriteToTopUpsDiagnosticsLog(Constants.CreateInfoLog, "Create_Folders()", LoanID, "Pack folder step completed" + PackName + "-" + PackNo);
                }
            });
        }

        catch (Exception e)
        {
            if (ConfigValues.LogTraceFlag == "true")
            {
                Diagnostics.WriteToTopUpsDiagnosticsLog(Constants.CreateErrorLog, "Create_LoanFolder()", LoanID, "Unable to Create Loan/Pack Folder-" + e.Message);
            }
            TechMessage = Constants.ERR_GENERAL + e.Message;
            StatusType = ResponseType.Error;
            StatusCode = ResponseType.ErrorCode_3;
            errormessage = e.Message;
            throw e;
        }
    }
    #endregion

    #region Read all the documents and its Properties
    private string StoreDocuments(XmlDocument xdoc, string LoanFolder, string Documentpath)
    {
        XPathNavigator nav;
        string strPackName = string.Empty;
        // string Docs;

        nav = xdoc.CreateNavigator();
        XmlElement xe = xdoc.DocumentElement;
        XmlNodeList lstTitles = xdoc.GetElementsByTagName("Message:Document");
        int no = lstTitles.Count;
        StringBuilder responseXML = new StringBuilder();
        string XMLresp = string.Empty;
        ArrayList DocumentNames = new ArrayList();

        if (ConfigValues.LogTraceFlag == "true")
        {
            Diagnostics.WriteToTopUpsDiagnosticsLog(Constants.CreateInfoLog, "StoreDocuments()", LoanFolder, "Creation of documents commenced Step commenced");
        }

        try
        {
            foreach (XmlNode node in lstTitles)
            {
                XmlNodeList prop = node.ChildNodes;
                int count = prop.Count;
                string Documentdata = string.Empty;
                string xmldocumentname = string.Empty;
                string xml_documentname = string.Empty;
                string xmlFormID = string.Empty;
                string xmlDateAndTime = string.Empty;
                string xmlPrintOrder = string.Empty;
                string xmlRenditionType = string.Empty;
                string created_date = string.Empty;
                string xmlEditable = string.Empty;
                string created_byuser = string.Empty;

                foreach (XmlNode document in prop)
                {
                    // to extract the document base 64 content
                    if (document.Name == "Message:Data")
                    {
                        Documentdata = document.FirstChild.InnerText;
                    }

                    // to extract the metadata related to each document
                    else
                    {
                        if (document.Name == "Message:KeywordIndex")
                        {
                            XmlNodeList keywordlist = document.ChildNodes;

                            foreach (XmlNode keywords in keywordlist)
                            {
                                string docattribute = keywords.Attributes["Name"].Value.ToString();
                                string sdocname = Constants.Attribute_DocumentName.ToString();
                                string sformid = Constants.Attribute_FormID.ToString();
                                string sdate = Constants.Attribute_Date.ToString();
                                string sprintorder = Constants.Attribute_PrintOrder.ToString();
                                string srendition = Constants.Attribute_Rendition.ToString();
                                string seditable = Constants.Attribute_Editable.ToString();

                                if (docattribute != null)
                                {
                                    switch (docattribute)
                                    {
                                        case "DocumentName":
                                            xmldocumentname = keywords.InnerText;
                                            break;
                                        case "FormID":
                                            xmlFormID = keywords.InnerText;
                                            break;
                                        case "DateAndTime":
                                            xmlDateAndTime = keywords.InnerText;
                                            break;
                                        case "PrintOrder":
                                            xmlPrintOrder = keywords.InnerText;
                                            break;
                                        case "RenditionType":
                                            xmlRenditionType = keywords.InnerText;
                                            break;
                                        case "Editable":
                                            xmlEditable = keywords.InnerText;
                                            break;
                                    }
                                }
                                else
                                {
                                    Diagnostics.WriteToTopUpsDiagnosticsLog(Constants.CreateInfoLog, "StoreDocuments()", LoanFolder, "Document Metadata is null-" + docattribute);
                                }
                            }
                        }
                    }
                }

                //Add documents to ArrayList

                DocumentNames.Add(
                    new DocumentsNameList
                    {
                        DocumentName = xmldocumentname,
                    });

                //Get count of same document names in array list
                var DocumentCount = from DocumentsNameList doc in DocumentNames
                                    where doc.DocumentName == xmldocumentname
                                    select doc;

                int NameCount = DocumentCount.Count();

                // Check if document exists in the directory
                if (xmldocumentname != null && Documentdata != null)
                {
                    string SharepointUrl = ConfigValues.SharePointRootUrl;

                    string SharepointPath = ConfigValues.DocumentLibraryName;
                    byte[] binaryData1;

                    // Converting base 64 string to byte array to be added in the document library
                    binaryData1 = System.Convert.FromBase64String(Documentdata);

                    //Add the document name value to a temporay variable inorder to create the response xml
                    xml_documentname = xmldocumentname;

                    //Get append count-1 value to the 2nd document such that the 1st document is <documentname>,
                    //2nd document is <documentname>-1 and 3rd is <documentname>-2 and so on
                    int DocCount = 0;

                    if (NameCount > 1)
                    {
                        DocCount = NameCount - 1;
                        xmldocumentname = xmldocumentname + "-" + DocCount;
                    }

                    // Add document properties to the hash table
                    Hashtable properties = new Hashtable();
                    properties.Add("FormID", xmlFormID);
                    properties.Add("RenditionType", xmlRenditionType);
                    properties.Add("Modified_User", "System Account");

                    DateTime date = DateTime.Now;
                    date = date.AddHours(-10);

                    properties.Add("CreatedDate", date);

                    if (xmldocumentname != "CBAEnvelope")
                    {
                        properties.Add("XMLDateTime", xmlDateAndTime);
                        properties.Add("OrderNo", xmlPrintOrder);
                        properties.Add("Restricted Type", xmlEditable);
                    }

                    properties.Add("FileDisplayStatus", "0");

                    if (xmldocumentname == "CBAEnvelope")
                    {
                        xmldocumentname = "Data File";
                    }
                    // properties.Add("DocumentName", xmldocumentname);

                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        SPSite _MySite = new SPSite(SharepointUrl + SharepointPath + "/" + LoanFolder);
                        SPWeb _MyWeb = _MySite.OpenWeb();
                        string PackFolderPath = SharepointUrl + SharepointPath + "/" + LoanFolder + "/" + Documentpath + "/";
                        SPFolder DocLibrary = _MyWeb.GetFolder(PackFolderPath);
                       // _MyWeb.AllowUnsafeUpdates = true;

                        //Add File name to the collection and get the file number

                        string SharepointFilepath = SharepointUrl + SharepointPath + "/" + LoanFolder + "/" + Documentpath;
                        SPFolder folder = _MyWeb.GetFolder(SharepointFilepath);
                        SPFileCollection fileCollection = folder.Files;

                        _MyWeb.AllowUnsafeUpdates = true;
                        _MySite.AllowUnsafeUpdates = true;

                        //Add file to the document library along with all the properties
                        SPFile file = DocLibrary.Files.Add(xmldocumentname + "." + xmlRenditionType, binaryData1, properties, false);

                         DocLibrary.Update();
                        _MyWeb.Update();

                        _MyWeb.AllowUnsafeUpdates = false;
                        _MySite.AllowUnsafeUpdates = false;

                        // Perform Clean Up
                        _MyWeb.Dispose();
                        _MySite.Dispose();
                    });

                    if (ConfigValues.LogTraceFlag == "true")
                    {
                        Diagnostics.WriteToTopUpsDiagnosticsLog("FileList", "StoreDocuments()",LoanFolder, xmldocumentname);

                    }
                }
        }

        catch (Exception e)
        {
            if (ConfigValues.LogTraceFlag == "true")
            {
                Diagnostics.WriteToTopUpsDiagnosticsLog(Constants.CreateErrorLog, "StoreDocuments()-error while creating Document" , LoanFolder, e.Message);
            }
            TechMessage = ResponseType.Error;
            TechMessage = Constants.ERR_GENERAL;
            StatusCode = ResponseType.ErrorCode_1;
            errormessage = e.Message;
            throw e;
        }


        xdoc = null;

        //return the <tns:DocumentInformation> tag contents values back to the calling function
        return XMLresp;
    }
    #endregion
share|improve this question
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.