I did a lot of research to retrieve/ upload list item attachments using Client object model but found no solutions. The alternative method is Web services. This method returns the result in the form of XML Node.
First to know about SharePoint Web Services please refer this.
In order to connect to the web service first add a web reference to your Visual Studio project. Refer this
Here is the sample code which works absolutely.
Adding attachment to SharePoint list item
http://www.naumanahmed.com/2012/03/sharepoint-web-services-and-dataset-how.html
First to know about SharePoint Web Services please refer this.
In order to connect to the web service first add a web reference to your Visual Studio project. Refer this
Here is the sample code which works absolutely.
Adding attachment to SharePoint list item
Retrieve attachment Details from SharePoint list itempublic string AddingAttachments(string Url, string username, string password, string DomainName, int itemID, string attachFile, string ListName){try{string attacnameName = "";byte[] contents;System.Net.NetworkCredential NC = new System.Net.NetworkCredential(username, password, DomainName);LService = new SharePointSvce.Lists();LService.Url = Url + "/_vti_bin/Lists.asmx";LService.Credentials = NC;System.IO.FileStream fStream = System.IO.File.OpenRead(attachFile);attacnameName = fStream.Name.Substring(3);contents = new byte[fStream.Length];fStream.Read(contents, 0, (int)fStream.Length);fStream.Close();try{string addAttach = LService.AddAttachment(ListName, itemID.ToString(), attacnameName, contents);}catch (System.Web.Services.Protocols.SoapException ex){return ("Message:\n" + ex.Message + "\nDetail:\n" + ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace);}}catch (Exception ex){return ex.Message;}return "";}
Download attachment from SharePoint list itempublic DataTable GetAvilableAttachmentsDetails(string Url, string username, string password, string DomainName, string SiteName, int taskID, string attachFile, string ListNAme){try{//Create tableDataTable dtAttachment = new DataTable();dtAttachment.Columns.Add("Name", typeof(string));dtAttachment.Columns.Add("SourceURL", typeof(string));System.Net.NetworkCredential NC = new System.Net.NetworkCredential(username, password, DomainName);LService = new SharePointSvce.Lists();LService.Url = currentURL + "/_vti_bin/Lists.asmx";LService.Credentials = NC;DataSet ds = new DataSet();XmlNode ndAttach = LService.GetAttachmentCollection(ListNAme, taskID.ToString());using (XmlNodeReader reader = new XmlNodeReader(ndAttach)){ds.ReadXml(reader);}if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0){for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++){string sourceUrl = Convert.ToString(ds.Tables[0].Rows[i][0]);int strLastIndx = sourceUrl.LastIndexOf(@"/");string FileName = sourceUrl.Substring(strLastIndx + 1);DataRow dr = dtAttachment.NewRow();dr["SourceURL"] = sourceUrl;dr["Name"] = FileName;dtAttachment.Rows.Add(dr);}}}catch (Exception){return null;}return dtAttachment;}
public string DownloadAttachment(string Url, string username, string password, string DomainName, string SiteName, int taskID, string attachFile, string ListNAme, string SourceURL, string SavefilePath)
{try{System.Net.NetworkCredential NC = new System.Net.NetworkCredential(username, password, DomainName);LService = new SharePointSvce.Lists();LService.Url = currentURL + "/_vti_bin/Lists.asmx";LService.Credentials = NC;using (WebClient client = new WebClient()){client.UseDefaultCredentials = false;client.Credentials = new NetworkCredential(username, password);byte[] response = client.DownloadData(SourceURL);int strLastIndx = SourceURL.LastIndexOf(@"/");string FileName = SourceURL.Substring(strLastIndx + 1);System.IO.FileStream fstream = System.IO.File.Create(@SavefilePath + "\\" + FileName);fstream.Write(response, 0, response.Length);fstream.Close();return "";}}catch (Exception ex){return ex.Message;}}
Reference : http://msdn.microsoft.com/en-us/library/lists.lists.addattachment%28v=office.12%29.aspx
http://www.naumanahmed.com/2012/03/sharepoint-web-services-and-dataset-how.html
thank you so much
ReplyDelete