Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Uploading Documents Programatically to a Content Organizer Enabled Site

, 23 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
CodeProject We have covered Content Organizer / Metadata Routing in SP2010 in earlier post.We have created all the required infrastructure for meta data routing or Content organizing. So only task remining is uploading the documents.Well ! we cannot ask the users to navigate to Drop Off library and

We have covered Content Organizer / Metadata Routing in SP2010 in earlier post. We have created all the required infrastructure for meta data routing or Content organizing. So the only task remining is uploading the documents. Well! We cannot ask the users to navigate to Drop Off library and upload their documents. Right? So I have created a simple Webpart for uploading the documents for the specific content type.

Now we need to upload the selected files programatically to Drop Off Library. Below is the code written by me for uplaoding the file to DropOff Library of the respective site.

 
private void UploadFromFileControl(FileUpload fuFile,string description)
        {
            using (SPWeb objWeb = SPContext.Current.Site.OpenWeb())
            {
                objWeb.AllowUnsafeUpdates = true;
                
                SPFolder myLibrary = objWeb.Folders[<yellow>"DropOffLibrary"</yellow>];
                Boolean replaceExistingFiles = true;
                String fileName = System.IO.Path.GetFileName(fuFile.PostedFile.FileName);
                Int32 fileLen = fuFile.PostedFile.ContentLength;
                Byte[] Input = new Byte[fileLen];
                Stream fileStream = fuFile.FileContent;
                fileStream.Read(Input, 0, fileLen);
                SPFile spFile = myLibrary.Files.Add(Path.GetFileName(fuFile.PostedFile.FileName), fileStream, replaceExistingFiles);

                SPListItem spListItem = spFile.ListItemAllFields;
                spListItem["ContentType"] = objWeb.ContentTypes["CntUploadedDocs"];
                spListItem["CandidateId"] = CandidateId;
                spListItem["Relationship"] = ddlRelation.Items[ddlRelation.SelectedIndex].Value.ToString();
                spListItem["DocumentType"] = ddlDocumentType.Items[ddlDocumentType.SelectedIndex].Value.ToString();
                spListItem["Description"] = description;
                spListItem.Update();
                spFile.Update();
            }
        }

Result: Code executed perfectly, and the Document is uploaded to DropOff Library. I opened the DropOff library and the record is successfully added. Task Acomplished?

No! Our task is not to add the document to DropOff library. Automatically it has to be moved from DropOff library to destination Library by Content organizer. But why it is still waiting there. Then I came across a stunning fact that ...

Content Organizer Rules will not be triggered for Documents added to DropOff Library Programatically

Now I went to Content Organizer settings and copied the webservice URL at the bottom. Usually it ends with "/vti_bin/OfficialFile.asmx". Reffered the web service in my visual studio solution with name "FileUploadSvc". Obviously you will end up writing code using "SPFile", "SPListItem" and "FileUploadSvcSoapClient". It is a tedious process of doing so, but later I realized that reffering OfficialFile.asmx will create a Circualr refrence issue. Reason being, the web service URL has to be used for configuring "SendTo" connections in Central Admin for access from other sites. Not for refering in your own website.

Now, I end up with a big question.

How to Upload Documents to Content Organizer Enabled Site Programtically and make the Content Oragnizer rule trigger automatically?

Here is the answer: Microsoft.Office.Policy.dll 

Add this DLL to your solution, and use "Microsoft.Office.RecordsManagement.RecordsRepository.dll" in your class file.

Doing so, you can access a class called "OfficialFileCore"  which will reffer the OfficialFile.asmx webservice by default. Try browsing the webservice and see what are the methods available.

Look at th last method, "SubmitFile". That is the method we need to use and MSDN has lot of information to offer about syntax and other stuff.
Microsoft link talking about Syntax.
Now i changed the code of my usercontrol to :

        
private void UploadFromFileControl(FileUpload fuFile,string description)
        {
            using (SPWeb objWeb = SPContext.Current.Site.OpenWeb())
            {               
                try
                {
                    string destination = null;
                    String fileName = System.IO.Path.GetFileName(fuFile.PostedFile.FileName);
                    Int32 fileLen = fuFile.PostedFile.ContentLength;
                    Byte[] Input = new Byte[fileLen];
                    Stream fileStream = fuFile.FileContent;
                    fileStream.Read(Input, 0, fileLen);

                    List<microsoft .sharepoint.recordsrepositoryproperty=".sharepoint.recordsrepositoryproperty"> properties = new List<microsoft .sharepoint.recordsrepositoryproperty=".sharepoint.recordsrepositoryproperty">();
    properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "Name", Type = "Text", Value = fileName });
    properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "CandidateId", Type = "Text", Value = CandidateId });
    properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "Relationship", Type = "Text", Value = ddlRelation.Items[ddlRelation.SelectedIndex].Value.ToString() });
    properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "DocumentType", Type = "Text", Value = ddlDocumentType.Items[ddlDocumentType.SelectedIndex].Value.ToString() });
   properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "Description", Type = "Text", Value = description });
   properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "ContentType", Type = "Text", Value = "CntUploadedDocs" });
   var result = OfficialFileCore.SubmitFile(objWeb, Input, properties.ToArray(), "CntUploadedDocs", fuFile.PostedFile.FileName, @"domain\pratap", false, out destination);

                }
                catch (Exception ex)
                {
                }
                
            }
        }

Before deploying the code, there is another step to perform to have a smooth execution. Go to Site Actions > Site Permissions > Add the Current or Configured user  to this group.

If you havent done above action, you will not be able to add the records to any library and the SubmitFile method will return "NotFound" response back as a result of execution.

Deployed the solution and added the record again.

DropOff Library

Destination Document Library

Task Acomplished. We are able to Add / Upload document programatically to a Content Organizer enabled site. Is it helpful for you? Kindly let me know your comments / Questions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
QuestionCan we do the same using Managed Client Object Model? Pinmemberkalpesh patil13-Dec-13 2:01 
AnswerRe: Can we do the same using Managed Client Object Model? PinmemberPratapReddyP14-Jan-14 12:51 
QuestionSOurce code Pinmemberchandrakhishma19-Apr-13 20:56 
Prathap, I have the same requirement in my project which needs to be done urgently. CAn I request you to share the complete code for this project? It would be really helpful
AnswerRe: SOurce code PinmemberPratapReddyP19-Apr-13 21:16 
QuestionUnknown error PinmemberkhKamel18-Dec-12 19:48 
AnswerRe: Unknown error PinmemberPratapReddyP18-Dec-12 20:22 
GeneralRe: Unknown error PinmemberkhKamel18-Dec-12 20:29 
GeneralRe: Unknown error PinmemberkhKamel18-Dec-12 20:45 
GeneralRe: Unknown error PinmemberPratapReddyP18-Dec-12 21:00 
GeneralRe: Unknown error PinmemberkhKamel18-Dec-12 21:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141030.1 | Last Updated 24 Nov 2012
Article Copyright 2012 by PratapReddyP
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid