private void CopyFileToList(string fileName, MemoryStream stream) { var copyService = new Copy(); var bytBytes = stream.ToArray(); var sFileCompletePath = this._parameters.SpSiteCollection + "/" + this._parameters.SpDocumentLibrary + "/" + fileName; try { var fields = new List <FieldInformation>(); CopyResult[] result; if (this._parameters.ImpersonateUser == "1") { var cred = new System.Net.NetworkCredential(this._parameters.SpUser, this._parameters.SpPassword, this._parameters.SpDomain); copyService.Credentials = cred; } else { copyService.Credentials = System.Net.CredentialCache.DefaultCredentials; } string[] destinationUrls = { Uri.EscapeUriString(sFileCompletePath) }; copyService.CopyIntoItems(sFileCompletePath, destinationUrls, fields.ToArray(), bytBytes, out result); if (result.First().ErrorCode.ToString() != "Success") { throw new Exception("Errore: " + result.First().ErrorMessage); } } catch { } }
/// <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> /// Experimental, should not be used as it is now. /// </summary> /// <param name="listUrl">URL to the list.</param> /// <param name="sourceFileStream">The file to be uploaded.</param> /// <param name="fileName">Filename.</param> /// <param name="fields">Fields with information about the file.</param> /// <returns>The id for the uploaded document.</returns> public int UploadItem(string listUrl, Stream sourceFileStream, string fileName, FieldInformation[] fields) { Copy copy = new Copy(); copy.Credentials = Credentials; copy.Url = Url.ToString().Replace("lists.asmx", "copy.asmx"); copy.Proxy = GetProxy(); ServicePointManager.ServerCertificateValidationCallback += ((sender, certificate, chain, sslPolicyErrors) => true); byte[] fileBytes = new byte[sourceFileStream.Length]; sourceFileStream.Read(fileBytes, 0, (int)sourceFileStream.Length); string[] destinationUri = { string.Format("{0}/{1}", listUrl, fileName) }; CopyResult[] result; int documentId = Convert.ToInt32(copy.CopyIntoItems("", destinationUri, fields, fileBytes, out result)); return(documentId); }
private Result <Person> ExportPicture(Person entity, R1Employee employee) { var results = new Result <Person>(); if (entity == null) { return(results.Merge(new Result <Person>(ResultType.ValidationError, "Missing Person Data"))); } if (employee == null) { return(results.Merge(new Result <Person>(ResultType.ValidationError, "Missing Employee Data"))); } if (entity.Image == null) { return(results.Merge(new Result <Person>(ResultType.ValidationError, "Missing Person Image"))); } var libraryName = (employee.EmployeeStatus.Equals('T') || employee.EmployeeStatus.Equals('R')) ? ExportConfig.InactiveEmployeeLibrary.TrimEnd('/') : ExportConfig.ActiveEmployeeLibrary.TrimEnd('/'); var destinationFile = string.Format("{0}/{1}/{2}.jpg", ExportConfig.Link.TrimEnd('/'), Uri.EscapeDataString(libraryName), Uri.EscapeDataString(employee.Initials.Trim())); var destinationUrls = new[] { destinationFile }; #region SharePoint Fields //Author Single line of text //Comments Multiple lines of text Image //Company Single line of text Image //Copyright Single line of text Image //Country Single line of text Image //Date Picture Taken Date and Time Image //Department Single line of text Image //DepartmentName Single line of text Image //Division Single line of text Image //EmployeeClassDesc Single line of text Image //EmployeeID Single line of text Image //EmployeeStatus Single line of text Image //EmployeeStatusDesc Single line of text Image //FirstName Single line of text Image //FullName Single line of text Image //Initials Single line of text Image //JobDescr Single line of text Image //Keywords Multiple lines of text Image //LastName Single line of text Image //LegalEntity Single line of text Image //MiddleName Single line of text Image //PhysicalLocation Single line of text Image //PhysicalLocationName Single line of text Image //Preview Image URL Hyperlink or Picture //ReportingLocation Single line of text Image //ReportingLocationName Single line of text Image //Scheduling End Date Publishing Schedule End Date //Scheduling Start Date Publishing Schedule Start Date //SupervisorID Single line of text Image //SupervisorInitials Single line of text Image //SupervisorName Single line of text Image //Created By Person or Group //Modified By Person or Group //Checked Out To Person #endregion FieldInformation[] metaData = { ServiceField("Company", employee.Company), ServiceField("Country", employee.Country), ServiceField("Department", employee.Department), ServiceField("DepartmentName", employee.DepartmentName), ServiceField("Division", employee.Division), ServiceField("EmployeeClassDesc", employee.EmployeeClassDesc), ServiceField("EmployeeID", employee.EmployeeID), ServiceField("EmployeeStatus", employee.EmployeeStatus.ToString()), ServiceField("EmployeeStatusDesc", employee.EmployeeStatusDesc), ServiceField("FirstName", employee.FirstName), ServiceField("FullName", employee.Name), ServiceField("Initials", employee.Initials), ServiceField("JobDescr", employee.JobDescr), ServiceField("LastName", employee.LastName), ServiceField("LegalEntity", employee.LegalEntity), ServiceField("MiddleName", employee.MiddleName), ServiceField("PhysicalLocation", employee.PhysicalLocation), ServiceField("PhysicalLocationName", employee.PhysicalLocationName), ServiceField("ReportingLocation", employee.ReportingLocation), ServiceField("ReportingLocationName", employee.ReportingLocationName), ServiceField("SupervisorID", employee.SupervisorID), ServiceField("SupervisorInitials", employee.SupervisorInitials), ServiceField("SupervisorName", employee.SupervisorName), ServiceField("ImageCreateDate", entity.udf1) }; try { var proxyWs = new Copy { Url = string.Format("{0}/_vti_bin/copy.asmx", ExportConfig.Link.TrimEnd('/')) }; if (string.IsNullOrWhiteSpace(ExportConfig.Username) || string.IsNullOrWhiteSpace(ExportConfig.Password)) { proxyWs.UseDefaultCredentials = true; } else { proxyWs.UseDefaultCredentials = false; proxyWs.Credentials = CreateCredentials(ExportConfig.Username, ExportConfig.Password, true); } CopyResult[] result; proxyWs.CopyIntoItems("http://null", destinationUrls, metaData, entity.Image, out result); if (result != null) { var row = 0; foreach (var copyResult in result.Where(copyResult => copyResult.ErrorCode != CopyErrorCode.Success)) { results.Fail(copyResult.ErrorMessage, string.Format("E{0}", row)); row++; } } return(results); } catch (Exception ex) { var errors = new Dictionary <string, string> { { "StackTrace", ex.StackTrace } }; return(results.Merge(new Result <Person>(ResultType.TechnicalError, ex.Message, errors))); } }