Showing posts with label
C# Application
.
Show all posts
Showing posts with label
C# Application
.
Show all posts
Wednesday, March 20, 2019
Upload Attachment to SharePoint Site List Item
Application Form Window
CODE ---------------------- using System; using System.Windows.Forms; using System.IO; using System.Security; using spClient = Microsoft.SharePoint.Client; namespace Upload_Attachment_SP_Online { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnBrowse_Click(object sender, EventArgs e) { var file = openFileDialog1.ShowDialog(); if (file.ToString() == "OK") txtFilePath.Text = openFileDialog1.FileName.ToString(); var x = txtFilePath.Text.Split('\\'); txtFileName.Text = x[x.Length - 1]; } private void btnUpload_Click(object sender, EventArgs e) { try { using (spClient.ClientContext ctx = new spClient.ClientContext("https://contoso.sharepoint.com/tc/")) { SecureString upwd = new System.Net.NetworkCredential("", "password1").SecurePassword; ctx.Credentials = new spClient.SharePointOnlineCredentials("jon.smith@contoso.com", upwd); spClient.Web myWeb = ctx.Web; spClient.List oList = myWeb.Lists.GetByTitle("TestList"); spClient.CamlQuery camlQuery = new spClient.CamlQuery(); camlQuery.ViewXml = "
" + "
Item1
"; spClient.ListItemCollection collListItem = oList.GetItems(camlQuery); ctx.Load(collListItem); ctx.ExecuteQuery(); spClient.AttachmentCreationInformation aci = new spClient.AttachmentCreationInformation(); byte[] bytes = File.ReadAllBytes(txtFilePath.Text); MemoryStream mStream = new MemoryStream(bytes); aci.ContentStream = mStream; aci.FileName = txtFileName.Text; foreach (spClient.ListItem oListItem in collListItem) { spClient.Attachment attachment = oListItem.AttachmentFiles.Add(aci); oListItem.Update(); } ctx.ExecuteQuery(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
Older Posts
Home
View mobile version
Subscribe to:
Posts (Atom)