Click here to Skip to main content
15,885,365 members
Articles / Productivity Apps and Services / Sharepoint / SharePoint 2010

Uploading Documents Programatically to a Content Organizer Enabled Site

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
23 Nov 2012CPOL3 min read 24.6K   3   10
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.

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.

This article was originally posted at http://pratapreddypilaka.blogspot.com/feeds/posts/default

License

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



Comments and Discussions

 
QuestionCan we do the same using Managed Client Object Model? Pin
kalpesh patil13-Dec-13 2:01
kalpesh patil13-Dec-13 2:01 
AnswerRe: Can we do the same using Managed Client Object Model? Pin
PratapReddyP14-Jan-14 12:51
PratapReddyP14-Jan-14 12:51 
QuestionSOurce code Pin
chandrakhishma19-Apr-13 20:56
chandrakhishma19-Apr-13 20:56 
AnswerRe: SOurce code Pin
PratapReddyP19-Apr-13 21:16
PratapReddyP19-Apr-13 21:16 
QuestionUnknown error Pin
khKamel18-Dec-12 19:48
khKamel18-Dec-12 19:48 
AnswerRe: Unknown error Pin
PratapReddyP18-Dec-12 20:22
PratapReddyP18-Dec-12 20:22 
GeneralRe: Unknown error Pin
khKamel18-Dec-12 20:29
khKamel18-Dec-12 20:29 
GeneralRe: Unknown error Pin
khKamel18-Dec-12 20:45
khKamel18-Dec-12 20:45 
GeneralRe: Unknown error Pin
PratapReddyP18-Dec-12 21:00
PratapReddyP18-Dec-12 21:00 
GeneralRe: Unknown error Pin
khKamel18-Dec-12 21:30
khKamel18-Dec-12 21:30 

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

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