2014-10-28 176 views
0

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

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();       
      } 
     } 
    });  
} 

回答

0

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

//exposureLibrary.Update();

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

+0

有任何进一步的帮助。我想根据某些条件从文档库中检索文档并将其显示为链接。当链接被点击时,文档应该被打开。 感谢您的帮助.. – user2028871 2014-11-11 07:52:50