Sharepoint 服务端对象模型增加项到文档库

新增Item到SP Document Library

前台添加FileUpload控件

<asp:FileUpload ID="fl_Resume" runat="server"  Width="550px" />

 

private bool addCVToDoc(string jd_ID)

        {

            bool isSuccess = false;

            string q1 = tb_Q1.Text;

            string q2 = tb_Q2.Text;

            string q3 = tb_Q3.Text;

            string q4 = tb_Q4.Text;

            string q5 = tb_Q5.Text;

            

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                using (SPSite site = new SPSite(SPContext.Current.Web.Url))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        web.AllowUnsafeUpdates = true;

                        SPDocumentLibrary list_JD = (SPDocumentLibrary)web.Lists[HRConstant.listCandidateCV];

                        SPListItem item = list_JD.Items.Add();
//避免重名

int lastDot = fl_Resume.FileName.LastIndexOf('.');
string fileName = fl_Resume.FileName.Substring(0, lastDot) + "_" + DateTime.Now.ToString("yyyyMMddHHmm") + fl_Resume.FileName.Substring(lastDot);

// Opening a filestream 

                        Stream fStream = fl_Resume.PostedFile.InputStream;

                        byte[] contents = new byte[fStream.Length];

                        fStream.Read(contents, 0, (int)fStream.Length);

                        fStream.Close();



                        var documentMetadata = new Hashtable

                            {

                                { "Q1", q1 },

                                { "Q2", q2 },

                                { "Q3", q3 },

                                { "Q4", q4 },

                                { "Q5", q5 },

                                { "JDID", jd_ID }

                            };

                        list_JD.RootFolder.Files.Add(fileName, contents, documentMetadata, true);

                        list_JD.Update();

                        web.AllowUnsafeUpdates = false;

                        isSuccess = true;

                    }

                }

            });

            return isSuccess;

        }

 

你可能感兴趣的:(SharePoint)