/// <summary>
        /// Creates the folder.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <param name="folderPath">The folder path.</param>
        /// <returns></returns>
        public string CreateFolder(string list, string folderPath, ProcessLog writeLog)
        {
            TraceManager.TraceInformation("WSSVC:CreateFolder:Adding folder <{0}> to SharePoint.", folderPath);
            Uri    folderUri  = new Uri(folderPath);
            string folderName = HttpUtility.UrlDecode(folderUri.Segments.Last().Trim());

            TraceManager.TraceInformation("\tWSSVC:CreateFolder:Idenfied folder name: {0}", folderName);
            if (!(string.IsNullOrEmpty(folderPath.Trim()) || string.IsNullOrEmpty(folderName)))
            {
                return(UpdateListItems(list, SharePointHelpers.CreateFolder(GetDefaultListView(list), folderName).ToXmlNode(), writeLog));
            }

            return(string.Empty);
        }
        /// <summary>
        /// Updates the list items.
        /// </summary>
        /// <param name="listName">Name of the list.</param>
        /// <param name="updates">The updates.</param>
        /// <returns></returns>
        public string UpdateListItems(string listName, XmlNode updates, ProcessLog process)
        {
            TraceManager.TraceInformation("WSSVC:UpdateListItems:List {0}", listName);
            string resultId = string.Empty;

            using (Lists listsWebService = new Lists())
            {
                listsWebService.Url         = string.Format(CultureInfo.CurrentCulture, "{0}/_vti_bin/lists.asmx", this.ServerUrl);
                listsWebService.Credentials = this.Credentials;

                XDocument resultsNode = XDocument.Parse(listsWebService.UpdateListItems(listName, updates).OuterXml);
                TraceManager.TraceInformation("WSSVC:UpdateListItems:Action completed");

                XNamespace sharePointNamespace = "http://schemas.microsoft.com/sharepoint/soap/";
                XElement   result = (from r in resultsNode.Descendants(sharePointNamespace + "Result")
                                     select r).First();

                string errorId = result.Descendants(sharePointNamespace + "ErrorCode").First().Value;
                if (!errorId.Equals("0x00000000"))
                {
                    string errorText = result.Descendants(sharePointNamespace + "ErrorText").First().Value;
                    TraceManager.TraceInformation("WSSVC:UpdateListItems:Error detected {0}:{1}", errorId, errorText);
                    throw new Exception(string.Format(CultureInfo.CurrentCulture, "{0} ({1})", errorText, errorId));
                }

                XNamespace rowsetSchemeXNamespace = "#RowsetSchema";

                XElement row = (from r in resultsNode.Descendants(rowsetSchemeXNamespace + "row")
                                where r.Attribute("ows_ID") != null
                                select r).First();

                resultId = row.Attribute("ows_ID").Value;
                process.Add(new Item()
                {
                    EncodedAbsUrl = row.Attribute("ows_EncodedAbsUrl").Value, Version = row.Attribute("ows_owshiddenversion").Value, Workspace = this.Workspace
                });
            }

            TraceManager.TraceInformation("WSSVC:UpdateListItems:Result ID: {0}", resultId);
            return(resultId);
        }
        /// <summary>
        /// Adds the file.
        /// </summary>
        /// <param name="targetUrl">The local path.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="documentLibrary">The document library.</param>
        /// <returns>The ID of the file in SharePoint</returns>
        public string AddFile(string targetUrl, string fileName, string documentLibrary, ProcessLog p)
        {
            string itemId = string.Empty;

            TraceManager.TraceInformation("\tWSSVC:AddFile:Adding file <{0}> to SharePoint {1}", fileName, targetUrl);
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("\t\tCannot find file to upload", fileName);
            }

            FieldInformation[] fields = new FieldInformation[] { new FieldInformation() };

            byte[]       fileContents    = File.ReadAllBytes(fileName);
            string[]     destinationUrls = { targetUrl };
            CopyResult[] results;

            using (Copy copyWebService = new Copy())
            {
                copyWebService.Url         = string.Format(CultureInfo.CurrentCulture, "{0}/_vti_bin/copy.asmx", this.ServerUrl);
                copyWebService.Credentials = this.Credentials;
                copyWebService.CopyIntoItems(targetUrl, destinationUrls, fields, fileContents, out results);

                foreach (CopyResult result in results)
                {
                    if (result.ErrorCode != CopyErrorCode.Success)
                    {
                        throw new Exception(string.Format("Failed to upload file {0} to SharePoint. Message: {1}", targetUrl, result.ErrorMessage));
                    }

                    using (Lists listsWebService = new Lists())
                    {
                        listsWebService.Url         = string.Format(CultureInfo.CurrentCulture, "{0}/_vti_bin/lists.asmx", this.ServerUrl);
                        listsWebService.Credentials = this.Credentials;
                        XNamespace rowsetSchemaNameSpace = "#RowsetSchema";

                        XDocument sharePointListItems = XDocument.Parse(listsWebService.GetListItems(documentLibrary, string.Empty, SharePointHelpers.QueryXmlNode, SharePointHelpers.ViewFieldsXmlNode, "0", SharePointHelpers.QueryOptionsXmlNode, string.Empty).OuterXml);
                        XElement  fileElement         = (from row in sharePointListItems.Descendants(rowsetSchemaNameSpace + "row")
                                                         where (row.Attribute("ows__CopySource") != null) &&
                                                         (row.Attribute("ows__CopySource").Value == targetUrl)
                                                         select row).First();

                        itemId = fileElement.Attribute("ows_ID").Value;
                        p.Add(new Item()
                        {
                            EncodedAbsUrl = fileElement.Attribute("ows_EncodedAbsUrl").Value, Version = fileElement.Attribute("ows_owshiddenversion").Value, Workspace = this.Workspace
                        });
                    }
                }
            }

            return(itemId);
        }
        /// <summary>
        /// Process a changegroup.
        /// </summary>
        /// <param name="changeGroup"></param>
        /// <returns></returns>
        public override ConversionResult ProcessChangeGroup(ChangeGroup changeGroup)
        {
            TraceManager.TraceInformation("WSSVC:MP:ProcessChangeGroup - {0}", changeGroup.Name);
            ProcessLog writeLog = new ProcessLog();

            Guid             targetSideSourceId = configurationService.SourceId;
            Guid             sourceSideSourceId = configurationService.MigrationPeer;
            ConversionResult convResult         = new ConversionResult(sourceSideSourceId, targetSideSourceId);

            try
            {
                foreach (MigrationAction action in changeGroup.Actions)
                {
                    TraceManager.TraceInformation("\t> {0} - {1}", action.Path, action.SourceItem.GetType().ToString());
                    if (action.Action == WellKnownChangeActionId.Add || action.Action == WellKnownChangeActionId.Edit)
                    {
                        if (action.ItemTypeReferenceName == WellKnownContentType.VersionControlledFile.ReferenceName)
                        {
                            string sharePointFileId = string.Empty;
                            string fileName         = Path.GetTempFileName();
                            File.Delete(fileName); // the download fails if the file exists
                            try
                            {
                                action.SourceItem.Download(fileName);
                                sharePointFileId = sharePointWriteUtilities.AddFile(action.Path, fileName, sourceIdentifier, writeLog);
                                convResult.ItemConversionHistory.Add(new ItemConversionHistory(changeGroup.Name, string.Empty, sharePointFileId, string.Empty));
                            }
                            finally
                            {
                                // cleanup temp file
                                if (File.Exists(fileName))
                                {
                                    File.Delete(fileName);
                                }
                            }
                        }

                        if (action.ItemTypeReferenceName == WellKnownContentType.VersionControlledFolder.ReferenceName)
                        {
                            string sharePointFileId = sharePointWriteUtilities.CreateFolder(sourceIdentifier, action.Path, writeLog);
                            convResult.ItemConversionHistory.Add(new ItemConversionHistory(changeGroup.Name, string.Empty, sharePointFileId, string.Empty));
                        }
                    }

                    if (action.Action == WellKnownChangeActionId.Delete)
                    {
                        if (action.ItemTypeReferenceName == WellKnownContentType.VersionControlledFile.ReferenceName ||
                            action.ItemTypeReferenceName == WellKnownContentType.VersionControlledFolder.ReferenceName)
                        {
                            sharePointWriteUtilities.Delete(action.Path);
                        }
                    }
                }
            }
            finally
            {
                writeLog.Save();
            }

            convResult.ChangeId = changeGroup.ReflectedChangeGroupId.ToString();
            return(convResult);
        }