How To Add A Document As An Attachment To A New SharePoint List Item In C#



TODO:

Have you ever wanted to add a file, as an attachment to a new item in a SharePoint list?

 

SOLUTION:

        /// <summary>Add the contents of a file to a document library</summary>
        /// <param name="DocumentData">byte[] of the document data</param>
        /// <param name="SharePointUrl">The URL to the sharepoint server</param>
        /// <param name="SharePointSite">The name of the site the document library resides</param>
        /// <param name="DocumentLibrary">The name of the document library</param>
        /// <param name="MetaData">Any meta data you want to add.  For instance if there are other columns, you can specify them here.</param>
        /// <param name="OverWriteFile">Overwrite flag.  true or false, self explanatory</param>
        /// <returns></returns>
        public bool AddFileToList(byte[] DocumentData, string SharePointUrl, string SharePointSite, string List, Hashtable MetaData)
        {
            try
            {
                //get the site
                using (SPSite mysiteCollection = new SPSite(SharePointUrl))
                {
                    // Alternately you can use oSite.RootWeb if you want to access the main site
                    // but we will just do a find so we do not need to worry about levels
                    SPWebInfo webinfo = mysiteCollection.AllWebs.WebsInfo.Find(delegate(SPWebInfo w) { return w.Title.ToUpper() == SharePointSite.ToUpper(); });
                    //get the site by title (need to because name could be site/site...)              
                    using (SPWeb rootSite = mysiteCollection.AllWebs[webinfo.Id])   //now get the web by GUID
                    {
                        SPList incompleteList = rootSite.Lists[List];               //the list by display name
                        SPListItem newItem = incompleteList.Items.Add();            //get the item from the file just uploaded

                        //now add each item to the properties
                        if (MetaData != null)
                        {
                            //take each piece of meta data, and apply it to the item
                            foreach (object key in MetaData.Keys)
                            {
                                //newItem.Properties.Add(key, MetaData[key]);
                                newItem[key.ToString()] = MetaData[key];
                            }
                        }

                        newItem.Attachments.Add(string.Format("{0}.xml", Guid.NewGuid()), DocumentData);   //add attachments to the list item
                        newItem.Update();   //update the item
                    }
                }
                return true;
            }
            catch (Exception x)
            {
                return false;
            }
        }

 

NOTES:

The meta data are columns on the item.  You can populate those during the add, by passing them in in a Hashtable.



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading