In the Document management space one of the
requirements is routing the documents automatically into appropriate
folders or sub folders inside a document library.
This article explains about the Out of box feature that is provided to meet the above requirement, the limitation of SharePoint 2010 and the Custom solution which meets the requirement

Click on OK will save the Content Organizer rule and Custom router will route the incoming documents based on the logic written in Custom Router
This article explains about the Out of box feature that is provided to meet the above requirement, the limitation of SharePoint 2010 and the Custom solution which meets the requirement
Introduction
In the Document
management space one of the requirements is routing the documents
automatically into appropriate folders or sub folders inside a document
library.
This article explains about the Out of box
feature that is provided to meet the above requirement, the limitation
of SharePoint 2010 and the Custom solution which meets the requirementOOTB SharePoint Option:
In SharePoint 2010 there is a feature called Content Organizer rule which addresses the above need through its attributes called Target location > Destination.
See those attributes in the below content organizer rule
Limitation with the content organizer rule routing
Whereas
there is a limitation with these attributes in terms of creating
hierarchical folders. For example if the requirement is to create
multiple levels of folders/subfolders inside the target location based
on the selected metadata it is not possible with the OOTB content
organizer rule. It can only create one level below the target location.
How to achieve this functionality through Custom Router?
We can leverage Custom
router to create the hierarchical folders and route the document
appropriately to the corresponding folder.
Custom router is the
custom class library that implements ICustomRouter interface and this
interface has one method called OnSubmitFil which needs to be
implemented by the custom router’s class.
Implementing Custom router:
Routing can be
customized by implementing an Interface called “ICustomRouter” which has
only one method named “OnSubmitFile”. This code will be called by
Content Organizer internally.
Here is the signature of OnSubmitFile
CustomRouterResult OnSubmitFile(
EcmDocumentRoutingWeb contentOrganizerWeb,
string recordSeries,
string userName,
Stream fileContent,
RecordsRepositoryProperty[] properties,
SPFolder finalFolder,
ref string resultDetails
)
Perform the below steps to implement this method to meet our requirement.
1. Retrieve the properties from the parameter called properties.
2. Push all the values into a Hashtable
3. Read the required properties based on which we need to create the hierarchical folder structure.
4. See whether this folder structure already exists.
5. Create the folder structure if it does not exist.
6. Move the file into the folder.
See the sample code below
public class MyCustomRouter : ICustomRouter
{
CustomRouterResult ICustomRouter.OnSubmitFile(
EcmDocumentRoutingWeb contentOrganizerWeb,
string recordSeries, // Content type name
string userName,
Stream fileContent,
Microsoft.SharePoint.RecordsRepositoryProperty[] properties,
SPFolder finalFolder,
ref string resultDetails)
{
string fileContentString = string.Empty;
StreamReader sr = new StreamReader(fileContent);
{
fileContentString = sr.ReadToEnd();
}
using (SPSite site = new SPSite(contentOrganizerWeb.DropOffZoneUrl))
{
SPWeb web = site.OpenWeb();
// User creating the file
// Create a Hashtable of properties which forms the metadata for the file
Hashtable fileProperties = EcmDocumentRouter.GetHashtableForRecordsReposi toryProperties(properties, recordSeries);
EcmDocumentRouter.SaveFileToFinalLocation(contentOrganizerWeb,
finalFolder,
finalStm,
fileName,
"",
fileProperties,
submittingUser,
true /*override versioning settings on the content organizer and create a new file*/, "");
}
resultDetails = "File was inspected and sensitive data was found. File has been saved with a custom name.";
return CustomRouterResult.SuccessCancelFurtherProcessing;
}
}
}
}
Now the custom router is ready to use. How this router will be attached to the Content Organizer?
Attaching Custom router to the Content Organizer rule:
The custom code
mentioned in the above section need to compile into an assembly and sign
the assembly with the key. Now the assembly is ready to add into the
GAC where SharePoint is running.
Add the assembly into GAC and it is ready to use inside the SharePoint.
Modify the Content Organizer rule:
Go to the Content
Organizer rule to which we want to attach the custom router. Edit
properties and go to Custom router field and select our newly created
Custom router assembly over there.
Click on OK will save the Content Organizer rule and Custom router will route the incoming documents based on the logic written in Custom Router