从sharepoint 2010文档库中检索文档

时间:2014-10-28 18:24:05

标签: sharepoint sharepoint-2010

我正在将文档上传到文档库。与此同时,我正在创建一个名为' FieldID'通过组合2个值。但是我无法在文档库中看到此字段,因此无法使用此字段查询文档。如何查看此字段或如何使用此字段ID'来检索文档。这是代码。

if (UploadFile.PostedFile != null || (UploadFile.PostedFile.ToString() != string.Empty))
{                               
    String fileName = UploadFile.PostedFile.FileName.ToString();                
    string userLogin = SPContext.Current.Web.CurrentUser.LoginName;                
    Guid siteID = SPContext.Current.Site.ID;
    SPDocumentLibrary exposureLibrary = null;
    SPList listDoc = null;
    SPFolder myLibrary = null;

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite oSite = new SPSite(siteID))
        {
            using (SPWeb oWeb = oSite.OpenWeb())
            {
                oWeb.AllowUnsafeUpdates = true;   
                // exposureLibrary
                try
                {
                   // Check if the document library already exists
                    exposureLibrary = (oWeb.Lists[SharepointCommon.Constants.USERINTERFACECUSTOMIZATIONSFEATURERECEIVER_EXPOSURE_DOCUMENTLIBRARY_NAME] as SPDocumentLibrary);

                    exposureLibrary.ContentTypesEnabled = false;
                    exposureLibrary.EnableAttachments = false;
                    exposureLibrary.EnableFolderCreation = false;
                    exposureLibrary.EnableVersioning = true;
                    exposureLibrary.NoCrawl = true;
                    exposureLibrary.OnQuickLaunch = false;
                    /* create a Text field for ID */


                    SPFieldText field = (exposureLibrary.Fields["FILEID"] as SPFieldText);

                // Check if the field is available
                    if (field == null)
                    {
                        SPFieldText fldID = (SPFieldText)exposureLibrary.Fields.CreateNewField(
                                        SPFieldType.Text.ToString(), "FILEID");
                        fldID.Required = true;
                        fldID.MaxLength = 100;
                        fldID.Hidden = false;
                        exposureLibrary.Fields.Add(fldID);
                    }
                    //exposureLibrary.Update();
                    myLibrary = exposureLibrary.RootFolder;

                }
                catch
                {

                    // Determine the GUID of the document library
                    Guid ExposureDocumentLibraryId = oWeb.Lists.Add(SharepointCommon.Constants.USERINTERFACECUSTOMIZATIONSFEATURERECEIVER_EXPOSURE_DOCUMENTLIBRARY_NAME, SharepointCommon.Constants.USERINTERFACECUSTOMIZATIONSFEATURERECEIVER_EXPOSURE_DOCUMENTLIBRARY_DESCRIPTION, SPListTemplateType.DocumentLibrary);

                    listDoc = oWeb.Lists[ExposureDocumentLibraryId];

                    /* create a Text field for ID */

                    SPFieldText fldID = (SPFieldText)listDoc.Fields.CreateNewField(
                                    SPFieldType.Text.ToString(), "FILEID");
                    fldID.Required = true;
                    fldID.MaxLength = 100;
                    fldID.Hidden = false;
                    listDoc.Fields.Add(fldID);

                    // Set properties of the document library
                    listDoc.ContentTypesEnabled = false;
                    listDoc.EnableAttachments = false;
                    listDoc.EnableFolderCreation = false;
                    listDoc.EnableVersioning = true;
                    listDoc.NoCrawl = true;
                    listDoc.OnQuickLaunch = false;                                
                    listDoc.Update();
                    myLibrary = listDoc.RootFolder;
                }


                // Prepare to upload
                Boolean replaceExistingFiles = true;


                // Upload document
                SPFile spfile = myLibrary.Files.Add(UploadFile.FileName + "(" + ViewState[UIConstants.CUSTOMERID].ToString() + " - "+ViewState[UIConstants.MondiPlantID].ToString()+ ")", UploadFile.FileContent, replaceExistingFiles);
                //spfile.Item["FILEID"]= ViewState[UIConstants.CUSTOMERID].ToString() + " _ " + ViewState[UIConstants.MondiPlantID].ToString();
                //myLibrary.Item["ID"] = ViewState[UIConstants.CUSTOMERID].ToString() + " _ " + ViewState[UIConstants.MondiPlantID].ToString();
                spfile.Item.Update();
                oWeb.AllowUnsafeUpdates = false;`enter code here`

                // Commit 
                myLibrary.Update();
                // Update the document library
                oWeb.Update();                           
            }
        }
    });    
}

1 个答案:

答案 0 :(得分:0)

exposureLibrary.Fields.Add(FLDID); //在此步骤之后

// exposureLibrary.Update();

您需要取消注释,因为在try块中创建字段后,列表/库未更新