2012-03-24 21 views
21

से किसी SharePoint सूची में कोई दस्तावेज़ अपलोड करें मुझे .NET (C#) से क्लाइंट साइड ऑब्जेक्ट मॉडल का उपयोग करके किसी SharePoint सूची या फ़ोल्डर में कोई दस्तावेज़ अपलोड करने की आवश्यकता है। इसे करने का बेहतरीन तरीका क्या है?क्लाइंट साइड ऑब्जेक्ट मॉडल

आवश्यकताओं इस प्रकार हैं:

  • सेट मेटाडाटा को महत्व देता

  • फ़ाइल के आकार पर कोई सीमा

  • पुस्तकालयों कि सूची दृश्य सीमा से अधिक के साथ काम करना होगा

+0

जावास्क्रिप्ट क्लाइंट साइड मॉडल या सामान्य ग्राहक मोड के बारे में पूछ रहे हैं: काम, यहाँ एक और संस्करण है कि पदोन्नत क्षेत्र के साथ एक कस्टम InfoPath पुस्तकालय के लिए wroks है एल? –

+0

^उन्होंने कहा कि वह .NET (सी #) सीएसओएम का उपयोग कर रहा है। – BrainSlugs83

उत्तर

19

SharePoin पर दस्तावेज़ अपलोड करने के लिए टी दस्तावेज़ लाइब्रेरी क्लाइंट ऑब्जेक्ट मॉडल में बाद समारोह का उपयोग करें:

public void UploadDocument(string siteURL, string documentListName, 
string documentListURL, string documentName, 

byte[] documentStream) 
{ 

using (ClientContext clientContext = new ClientContext(siteURL)) 
{   

//Get Document List 
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName); 

var fileCreationInformation = new FileCreationInformation(); 
//Assign to content byte[] i.e. documentStream 

fileCreationInformation.Content = documentStream; 
//Allow owerwrite of document 

fileCreationInformation.Overwrite = true; 
//Upload URL 

fileCreationInformation.Url = siteURL + documentListURL + documentName; 
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
    fileCreationInformation); 

//Update the metadata for a field having name "DocType" 
uploadFile.ListItemAllFields["DocType"] = "Favourites"; 

uploadFile.ListItemAllFields.Update(); 
clientContext.ExecuteQuery(); 

} 
} 

नीचे दिए गए लिंक के लिए आप 1) http://blogs.msdn.com/b/sridhara/archive/2010/03/12/uploading-files-using-client-object-model-in-sharepoint-2010.aspx

2) http://msdn.microsoft.com/en-us/library/ee956524.aspx

3) http://www.codeproject.com/Articles/103503/How-to-upload-download-a-document-in-SharePoint-20

+0

धन्यवाद। प्रश्न का पालन करें: पहला लिंक बताता है कि अपलोड सीमा को कैसे बदला जाए, लेकिन यह सर्वर साइड कोड है। क्या आप जानते हैं कि इस तरह सर्वर साइड कोड कैसे लिखना और तैनात करना है? क्या हम एक साधारण स्क्रिप्ट लिख सकते हैं और इसे सर्वर पर चला सकते हैं? सीमा बदलने के लिए कोई अन्य तरीका है (क्लाइंट साइड शायद?)? –

+0

दुर्भाग्य से, यह कोड शेयरपॉइंट में 1.5 एमबी या बड़ी फ़ाइलों को अपलोड करने की अनुमति नहीं देता है। इसके लिए आपको SaveBindaryDirect विधि, REST, या FileCreationInformation.ContentStream का उपयोग करने की आवश्यकता है। –

+0

मुझे पता है कि जब मैं वर्जनिंग के साथ इस तरह से एक फ़ाइल अपलोड करता हूं, तो मुझे प्रत्येक अद्यतन पर दो अलग-अलग संस्करण मिलते हैं: एक बार जब फ़ाइल और अन्य को जोड़ते समय ListItemAllFields.Update() – Kristopher

13

एक और तरीका है इसके अलावा सहायक है SaveBinaryDirect विधि का उपयोग करने के लिए। SaveBinaryDirect विधि फ़ाइलों को अपलोड और डाउनलोड करने के लिए वेब आधारित वितरित लेखांकन और संस्करण (WebDAV) का उपयोग करें। अपनी खुद की कस्टम डब्ल्यूसीएफ सेवा के निर्माण के बिना, वेबएडीवी फाइल अपलोड और डाउनलोड करने का सबसे प्रभावी तरीका है।

using (FileStream fs = new FileStream(FileToImport, FileMode.OpenOrCreate)) 
{ 
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, uri.LocalPath, fs, true); 
} 
Microsoft.SharePoint.Client.File newFile = web.GetFileByServerRelativeUrl(uri.LocalPath); 
context.Load(newFile); 
context.ExecuteQuery(); 

//check out to make sure not to create multiple versions 
newFile.CheckOut(); 

ListItem item = newFile.ListItemAllFields; 
item["Created"] = info.SourceFile.CreationTime; 
item["Modified"] = info.SourceFile.LastWriteTime; 
item.Update(); 

// use OverwriteCheckIn type to make sure not to create multiple versions 
newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn); 
+1

फ़ाइल में जांच करना संशोधित फ़ील्ड अपडेट करेगा दिनांक/समय पर जिस पर फ़ाइल की जांच की गई थी, इसलिए आपका उदाहरण अपेक्षित रूप से पूरी तरह से काम नहीं करेगा। – Eccentropy

7

अभी तक (शेयरपॉइंट ऑनलाइन सहित) File.SaveBinaryDirect Method का उपयोग कर एक SharePoint साइट में एक फ़ाइल अपलोड करने के लिए एक और विकल्प:

/// <summary> 
/// Uploads the specified file to a SharePoint site 
/// </summary> 
/// <param name="context">SharePoint Client Context</param> 
/// <param name="listTitle">List Title</param> 
/// <param name="fileName">File Name</param> 
private static void UploadFile(ClientContext context, string listTitle,string fileName) 
{ 
    using (var fs = new FileStream(fileName, FileMode.Open)) 
    { 
      var fi = new FileInfo(fileName); 
      var list = context.Web.Lists.GetByTitle(listTitle); 
      context.Load(list.RootFolder); 
      context.ExecuteQuery(); 
      var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name); 

      Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fileUrl, fs, true); 
     } 
    } 
+1

सुरुचिपूर्ण .. धन्यवाद! – Colbs

6

मैंने पाया कि delax पद वाले हिस्से की नई फ़ाइल को अद्यतन करता विशेषताओं/कॉलम नहीं होगा

public string AddNewForm(string WebUrl, string NewTitle) 
    { 
     string strMsg = ""; 
     if (string.IsNullOrEmpty(WebUrl)) 
      return EmptyProcURL; 

     try 
     { 
      // Starting with ClientContext, the constructor requires a URL to the server running SharePoint. 
      using (ClientContext client = new ClientContext(WebUrl)) 
      { 
       //client.Credentials = System.Net.CredentialCache.DefaultCredentials; 

       // Assume that the web site has a library named "FormLibrary". 
       var formLib = client.Web.Lists.GetByTitle("FormLibrary"); 
       client.Load(formLib.RootFolder); 
       client.ExecuteQuery(); 

       // FormTemplate path, The path should be on the local machine/server ! 
       string fileName = @"D:\Projects\FormTemplate.xml"; 

       var fileUrl = ""; 

       //Craete FormTemplate and save in the library. 
       using (var fs = new FileStream(fileName, FileMode.Open)) 
       { 
        var fi = new FileInfo("newForm.xml"); 
        fileUrl = String.Format("{0}/{1}", formLib.RootFolder.ServerRelativeUrl, fi.Name); 
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(client, fileUrl, fs, true); 
       } 

       // Get library columns collection. 
       var libFields = formLib.Fields; 
       client.Load(libFields); 
       client.ExecuteQuery(); 

       Microsoft.SharePoint.Client.File newFile = client.Web.GetFileByServerRelativeUrl(fileUrl); 

       ListItem item = newFile.ListItemAllFields; 

       // Here the index of Title column is 9, you may use this format to update any column (even promoted fields). 
       // To find the index of interested column you should inspect libFields at debug mode, look in the libFields.Fields collection to find the index! 
       item[libFields[9].StaticName] = NewTitle ; 
       item.Update(); 
       client.ExecuteQuery(); 
      } 
     } 
     catch (Exception ex) 
     { 
      strMsg = ex.Message; 
     } 

     return strMsg; 
    } 
संबंधित मुद्दे