18 February 2011

Re-parenting SharePoint Objects in SharePoint

While you do a backup of a SharePoint child object, the backed up object expect the same parent object at the restored location. We can over ride this default behavior of the SharePoint object by a practice called Re-Parenting. This helps to avoid keeping the same hierarchy at the Source and target while backup restore or Import /export process.
The property called “TargetParentUrl” need to be setted inorder to change the parent object.

How do we do this is as follows

1.Create a method which need to hooked while importing.

private void OnImportStarted(object sender, SPDeploymentEventArgs args)
{
string targetParentUrl = SPImportObjectCollection rootObjects = args.RootObjects;
foreach (SPImportObject io in rootObjects)
{
io.TargetParentUrl = targetParentUrl;
}
}
2. Call the method as follows by using SpImport Object
SPImport import = new SPImport(importSettings);
//event handler to change the parent EventHandler eventHandler =
new EventHandler(OnImportStarted);
import.Started += eventHandler;
import.Run();
Note : Howerver You Cannot Re-Parent Object to a different parent template other than its original while Import/Export.

No comments:

Post a Comment