Saturday, May 30, 2009

Adding a list item to Document library through c# in SharePoint 2007

As we all know the required filed for document library is not Title. We should be upload a file for the document library list item. This is the required field by default. You can add columns and make them required as well. So, when through program we want to add a list item to the document library then we need to collect the uploaded file and the meta data i.e. the list of columns data. So on the custom form of the document library we need to add ASP.NET controls for file upload and for all the other columns. Here I thought of giving C# code we need to use to create a list item in SharePoint document library. I think this is the only efficient way of adding a list item to document library. If not please post your ideas.

string fileName = fileUpload.PostedFile.FileName;
        using (SPSite site = new SPSite("http://sharepointserver"))
        {
            using (SPWeb web = site.OpenWeb("/"))
            {
                try
                {
                    web.AllowUnsafeUpdates = true;
                    using (FileStream fs = File.Open(fileName, FileMode.Open))
                    {
                        SPList list = web.Lists["Documents"];
                        Hashtable metaData = new Hashtable();
                        for (int i = 0; i < keys.Count; i++)
                        {
                            metaData.Add(keys[i], values[i]);
                        }
                        SPFile destfile = list.RootFolder.Files.Add(fileName.Substring(fileName.LastIndexOf("\\") + 1),
                            fs, metaData, true);
                        if (destfile == null)
                            lit.Text = "Error in adding file";
                    }
                }
                catch
                { }
                finally
                {
                    web.AllowUnsafeUpdates = false;
                }
            }
        }

NOTE: fileUpload is the control I am using in my code to get the file from the custom form. And the key and value fields are the column label and it’s value respectively. If you know the list column names then remove for loop and hard code the label, value while adding them to the metadata. Example like below code,

metaData.Add("Title", "DemoDocument"); metaData.Add("Version", "1.0"); metaData.Add("Author", "Praveen"); metaData.Add("ContentType", "Document");

NOTE: There is a problem in adding the list item through the code if your list columns are not inheriting from the data types string, int and datetime. You will get an exception. So follow this post to fix it.

Please post your ideas on it.

3 comments:

  1. Hi!
    You should use Path.GetFilename instead of custom code ;)

    ReplyDelete
  2. For what we need to use Path.GetFileName()? Can you tell me where we need to use it in this post?

    ReplyDelete
  3. I tried this code but the metadata i supplied is not saved in the list. Can you tell me if what is problem with it.

    ReplyDelete