如何在以编程方式上载文档后为sharepoint中的列添加名称

时间:2011-11-06 10:14:11

标签: sharepoint-2010

我可以通过以下代码将文档上传到sharepoint。我的文档库有3个必填列,“项目标题”等。代码是什么,以确保当我成功上传文档时,列数据也会反映出来。

谢谢!

    protected void Button2_Click(object sender, EventArgs e)
    {
        SPSite mySite = SPContext.Current.Site;
        SPWeb myWeb = mySite.OpenWeb();
        DocumentAttached(myWeb);
    }

    //The Function for uploading a File in a Document Library is Below:
    public void DocumentAttached(SPWeb site)
    {
        if (FileUpload1.HasFile)
        {
            SPFolder folder = site.GetFolder("Innovation Submission");
            SPFileCollection files = folder.Files;
            Stream fStream = FileUpload1.PostedFile.InputStream; //path of the file to upload
            byte[] contents = new byte[fStream.Length];
            fStream.Position = 0;
            fStream.Read(contents, 0, (int)fStream.Length);
            fStream.Close();
            string Filename = FileUpload1.FileName;
            string URL = SPContext.Current.Site.Url + "/RIDepartment/Innovation%20Submission/" + Filename;
            SPFile currentFile = files.Add(URL, contents);
        }
    }

2 个答案:

答案 0 :(得分:0)

如果要在上传文档后分配自定义字段,可以使用SPFile.Item属性,即:

SPListItem item = currentFile.Item;
item["Project Title"]="New title";
item.Update();

答案 1 :(得分:0)

查找将Hashtable作为参数的Add方法重载。您可以使用它来传递元数据列值。