static void Main(string[] args) { try { Console.WriteLine("start"); byte[] b = System.IO.File.ReadAllBytes(@"[PATH TO FILE LIKE C:\test.png]"); using (ClientContext context = new ClientContext("[SITECOLLECTION URL]")) { List list = context.Web.Lists.GetByTitle("[LIB NAME LIKE 'DOCUMENTS']"); FileCreationInformation fileInfo = new FileCreationInformation(); fileInfo.Content = b; fileInfo.Overwrite = true; fileInfo.Url = "[SITECOLLECTION URL]" + "/[LIB NAME FROM URL LIKE 'DOCUMENTS']/" + "[FILE NAME LIKE 'test.png']"; Microsoft.SharePoint.Client.File uploadFile = list.RootFolder.Files.Add(fileInfo); uploadFile.ListItemAllFields.Update(); context.ExecuteQuery(); } Console.WriteLine("end"); } catch (Exception ex) { Console.WriteLine("error -> " + ex.Message); } finally { Console.ReadLine(); } }
public SpIDisPaly(Microsoft.SharePoint.Client.File file) : this() { InitializeComponent(); txtDisplay.Text = file.Name; txtCreate.Text = file.Author.Title; txtEdit.Text = file.ModifiedBy.Title; txtFilePath.Text = file.ServerRelativeUrl; txtCreationTime.Text = file.TimeCreated.ToString(); txtModifyTime.Text = file.TimeLastModified.ToString(); txtFileType.Text = System.IO.Path.GetExtension(file.ServerRelativeUrl).Replace(".", ""); txtFileVersion.Text = file.UIVersionLabel; txtFileVersionsCount.Text = file.Versions.Count.ToString(); }
/// <summary> /// Uplaod file to library /// </summary> /// <param name="list">List to be processed - can be root web or sub site</param> /// <param name="filePath">Path to source location like c:\fuu.txt</param> public static void UploadDocumentToLibrary(this List list, string filePath) { using (FileStream fs = new FileStream(filePath, FileMode.Open)) { FileCreationInformation flciNewFile = new FileCreationInformation(); // This is the key difference for the first case - using ContentStream property flciNewFile.ContentStream = fs; flciNewFile.Url = System.IO.Path.GetFileName(filePath); flciNewFile.Overwrite = true; Microsoft.SharePoint.Client.File uploadFile = list.RootFolder.Files.Add(flciNewFile); list.Context.Load(uploadFile); list.Context.ExecuteQuery(); } }
/// <summary> /// Delete a file from SharePoint storage. /// </summary> /// <returns> /// This will return true unless an error occurs in which case, it will return a false. /// </returns> public bool Delete() { var result = true; errorMessage = string.Empty; try { var web = SessionHelper.Client.Web; var fileUri = new Uri(Core.RootUrl + Encode(path)); Microsoft.SharePoint.Client.File file = web.GetFileByServerRelativeUrl(fileUri.AbsolutePath); file.DeleteObject(); SessionHelper.Client.ExecuteQuery(); } catch (Exception exception) { errorMessage = exception.Message; result = false; } return(result); }
/// <summary> /// Upload document to folder /// </summary> /// <param name="web">Web to be processed - can be root web or sub site</param> /// <param name="filePath">Full path to the file like c:\temp\fuu.txt</param> /// <param name="folder">Folder Name in the site</param> public static void UploadDocumentToFolder(this Web web, string filePath, Folder folder) { if (!folder.IsObjectPropertyInstantiated("ServerRelativeUrl")) { web.Context.Load(folder); web.Context.ExecuteQuery(); } using (FileStream fs = new FileStream(filePath, FileMode.Open)) { FileCreationInformation flciNewFile = new FileCreationInformation(); // This is the key difference for the first case - using ContentStream property flciNewFile.ContentStream = fs; flciNewFile.Url = System.IO.Path.GetFileName(filePath); flciNewFile.Overwrite = true; Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(flciNewFile); folder.Context.Load(uploadFile); folder.Context.ExecuteQuery(); } }
/// <summary> /// Initializes the new instance of <see cref="SPClient.SPClientListItemConvertPipeBind"/> class. /// </summary> /// <param name="file">the file which converts to a list item.</param> public SPClientListItemConvertPipeBind(Microsoft.SharePoint.Client.File file) { this.ClientObject = file; }
protected void CreateDocumentLink_Click(object sender, EventArgs e) { FileStream fs = null; try { // When the user has selected a library, they will be allowed to click the button // The first thing we'll do is get the target library and its root folder. targetLibrary = hostingWeb.Lists.GetByTitle(OutputLibrary.SelectedItem.Text); Microsoft.SharePoint.Client.Folder destintationFolder = targetLibrary.RootFolder; clientContext.Load(destintationFolder); clientContext.ExecuteQuery(); // Then we'll build a Word Document by using OOXML // Note that we'll first create it in a folder in this Web app. using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(Server.MapPath("~/SampleOOXML/LocalOOXMLDocument.docx"), WordprocessingDocumentType.Document)) { // Add a main document part. MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); // Create the document structure. mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); // Create a paragraph. Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); run.AppendChild(new Text("Here's some text in a paragraph")); // Create a table. DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table(); // Create some table border settings. TableProperties borderProperties = new TableProperties( new TableBorders( new TopBorder { Val = new EnumValue <BorderValues>(BorderValues.DashDotStroked), Size = 12 }, new BottomBorder { Val = new EnumValue <BorderValues>(BorderValues.DashDotStroked), Size = 12 }, new LeftBorder { Val = new EnumValue <BorderValues>(BorderValues.DashDotStroked), Size = 12 }, new RightBorder { Val = new EnumValue <BorderValues>(BorderValues.DashDotStroked), Size = 12 }, new InsideHorizontalBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 12 }, new InsideVerticalBorder { Val = new EnumValue <BorderValues>(BorderValues.Single), Size = 12 })); // Add the table border settings to the table. table.AppendChild <TableProperties>(borderProperties); // Create a table row and add two cells with some text var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow(); var tc1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(); tc1.Append(new Paragraph(new Run(new Text("Here's some text in table cell #1")))); var tc2 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(); tc2.Append(new Paragraph(new Run(new Text("Here's some text in table cell #2")))); tr.Append(tc1); tr.Append(tc2); // Add the row to the table, and the table to the body of the document. table.Append(tr); body.Append(table); } // At this stage, the local file has been created in the folder of this Web project // so we'll now read it and create a new file in SharePoint, based on this local file. byte[] documentBytes; fs = File.OpenRead(Server.MapPath("~/SampleOOXML/LocalOOXMLDocument.docx")); documentBytes = new byte[fs.Length]; fs.Read(documentBytes, 0, Convert.ToInt32(fs.Length)); // At this stage, the file contents of the OOXML document has been read into the byte array // so we can use that as the content of a new file in SharePoint. Microsoft.SharePoint.Client.FileCreationInformation ooxmlFile = new Microsoft.SharePoint.Client.FileCreationInformation(); ooxmlFile.Overwrite = true; ooxmlFile.Url = hostingWeb.Url + destintationFolder.ServerRelativeUrl + "/SharePointOOXMLDocument.docx"; ooxmlFile.Content = documentBytes; Microsoft.SharePoint.Client.File newFile = targetLibrary.RootFolder.Files.Add(ooxmlFile); clientContext.Load(newFile); clientContext.ExecuteQuery(); // Let the user navigate to the document library where the file has been created string targetUrl = hostingWeb.Url + destintationFolder.ServerRelativeUrl; DocumentLink.Text = "Document has been created in SharePoint! Click here to view the library"; DocumentLink.Visible = true; DocumentLink.NavigateUrl = targetUrl; } catch (Exception ex) { // Tell the user what went wrong DocumentLink.Text = "An error has occurred: " + ex.Message; DocumentLink.Visible = true; DocumentLink.NavigateUrl = ""; } finally { // Clean up our filestream object fs.Close(); } }
internal void SPUploader(Stream fs, string fn) { ClientContext context = new ClientContext("http://Sharepointsite");///SitePages/Home.aspx"); System.Net.ICredentials creds = System.Net.CredentialCache.DefaultCredentials; context.Credentials = creds; context.RequestTimeout = 60000000; // Time in milliseconds string url = "/Members/"; string fileName = Path.GetFileName(fn); string fnUrl = url + fn; Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true); string uniqueRefNo = GetNextDocRefNo(); SP.Web web = context.Web; Microsoft.SharePoint.Client.File newFile = web.GetFileByServerRelativeUrl(fnUrl); context.Load(newFile); context.ExecuteQuery(); Web site = context.Web; List docList = site.Lists.GetByTitle("Members"); context.Load(docList); context.ExecuteQuery(); context.Load(docList.Fields.GetByTitle("Workflow Number")); context.Load(docList.Fields.GetByTitle("Agreement Number")); context.Load(docList.Fields.GetByTitle("First Name")); context.Load(docList.Fields.GetByTitle("Surname")); context.Load(docList.Fields.GetByTitle("ID Number")); context.Load(docList.Fields.GetByTitle("Date of Birth")); context.Load(docList.Fields.GetByTitle("Country")); context.Load(docList.Fields.GetByTitle("Document Description")); context.Load(docList.Fields.GetByTitle("Document Type")); context.Load(docList.Fields.GetByTitle("Document Group")); context.ExecuteQuery(); *********NEED TO GET THE INTERNAL COLUMN NAMES FROM SHAREPOINT************ var name = docList.Fields.GetByTitle("Workflow Number").InternalName; var name2 = docList.Fields.GetByTitle("Agreement Number").InternalName; var name3 = docList.Fields.GetByTitle("First Name").InternalName; var name4 = docList.Fields.GetByTitle("Surname").InternalName; var name5 = docList.Fields.GetByTitle("ID Number").InternalName; var name6 = docList.Fields.GetByTitle("Date of Birth").InternalName; var name7 = docList.Fields.GetByTitle("Country").InternalName; var name8 = docList.Fields.GetByTitle("Document Description").InternalName; var name9 = docList.Fields.GetByTitle("Document Type").InternalName; var name10 = docList.Fields.GetByTitle("Document Group").InternalName; var name11 = docList.Fields.GetByTitle("Unique Document Ref No").InternalName; **********NOW SAVE THE METADATA TO SHAREPOINT********** newFile.ListItemAllFields[name] = "927015"; newFile.ListItemAllFields[name2] = "1234565"; newFile.ListItemAllFields[name3] = "Joe"; newFile.ListItemAllFields[name4] = "Soap"; newFile.ListItemAllFields[name5] = "7502015147852"; newFile.ListItemAllFields[name6] = "1975-02-01"; newFile.ListItemAllFields[name7] = "ZA"; newFile.ListItemAllFields[name8] = "Test"; newFile.ListItemAllFields[name9] = "Requirements"; newFile.ListItemAllFields[name10] = "Requirements"; newFile.ListItemAllFields[name11] = uniqueRefNo; newFile.ListItemAllFields.Update(); context.Load(newFile); context.ExecuteQuery();