private void SetUpMapping(String mappingListUrl, String mappingListView) { if (String.IsNullOrEmpty(mappingListUrl)) { ErrorMessage("There was no mapping list URL specified."); return; } using (SPSite site = new SPSite(mappingListUrl)) using (SPWeb web = site.OpenWeb()) { SPList mappingList = web.GetList(mappingListUrl); SPListItemCollection mappingItems = null; if (String.IsNullOrEmpty(mappingListView)) { mappingItems = mappingList.Items; } else { SPView mappingView = mappingList.Views[mappingListView]; mappingItems = mappingList.GetItems(mappingView); } foreach (SPListItem mappingItem in mappingItems) { WBMappedPath mappedPath = new WBMappedPath(this, mappingItem); if (_mapping.ContainsKey(mappedPath.MappingPath)) { mappingItem.WBxSet(WBColumn.MigrationStatus, "Duplicate"); mappingItem.Update(); } else { _mapping.Add(mappedPath.MappingPath, mappedPath); } } } }
private void MigrateOneDocumentToLibrary( WBMigrationMapping mapping, SPSite sourceSite, SPWeb sourceWeb, SPDocumentLibrary sourceLibrary, SPSite destinationSite, SPWeb destinationWeb, SPFolder destinationRootFolder, SPSite controlSite, SPWeb controlWeb, SPList controlList, SPView controlView, SPListItem migrationItem) { WBFarm farm = WBFarm.Local; //foreach (SPField field in migrationItem.Fields) //{ // WBLogging.Migration.Verbose("Field InternalName: " + field.InternalName + " Field Title: " + field.Title + " item[field.Title] : " + migrationItem[field.Title]); //} String sourceFilePath = migrationItem.WBxGetAsString(WBColumn.SourceFilePath); String mappingPath = WBUtils.NormalisePath(migrationItem.WBxGetAsString(WBColumn.MappingPath)); WBLogging.Migration.Verbose("Trying to migrate file : " + sourceFilePath); WBLogging.Migration.Verbose("Migrating with mapping path : " + mappingPath); WBMappedPath mappedPath = mapping[mappingPath]; SPListItem controlItem = migrationItem; SPListItem mappingItem = null; SPListItem subjectItem = null; String documentumSourceID = ""; if (MigrationSourceSystem == MIGRATION_SOURCE__DOCUMENTUM_WEB_DOCUMENTS) { documentumSourceID = controlItem.WBxGetAsString(WBColumn.SourceID); if (!String.IsNullOrEmpty(documentumSourceID)) { mappingItem = WBUtils.FindItemByColumn(controlSite, MigrationMappingList, WBColumn.SourceID, documentumSourceID); subjectItem = WBUtils.FindItemByColumn(controlSite, MigrationSubjectsList, WBColumn.SourceID, documentumSourceID); } } if (mappedPath.InErrorStatus) { WBLogging.Migration.HighLevel("WBMigrationTimerJob.MigrateOneDocumentToLibrary(): There was an error with the mapped path: " + mappedPath.ErrorStatusMessage); return; } // OK so let's first get the various WBTerms from the mapped path so that if these // fail they fail before we copy the document! WBRecordsType recordsType = null; WBTermCollection <WBTerm> functionalArea = null; WBTermCollection <WBSubjectTag> subjectTags = null; if (MigrationSourceSystem == MIGRATION_SOURCE__DOCUMENTUM_WEB_DOCUMENTS) { string recordsTypePath = controlItem.WBxGetAsString(WBColumn.RecordsTypePath); if (String.IsNullOrEmpty(recordsTypePath) && mappingItem != null) { recordsTypePath = mappingItem.WBxGetAsString(WBColumn.RecordsTypePath); } Term rterm = mapping.RecordsTypesTaxonomy.GetSelectedTermByPath(recordsTypePath); if (rterm != null) { recordsType = new WBRecordsType(mapping.RecordsTypesTaxonomy, rterm); } string functionalAreaPath = controlItem.WBxGetAsString(WBColumn.FunctionalAreaPath); if (String.IsNullOrEmpty(functionalAreaPath) && mappingItem != null) { functionalAreaPath = mappingItem.WBxGetAsString(WBColumn.FunctionalAreaPath); } if (!String.IsNullOrEmpty(functionalAreaPath)) { string[] paths = functionalAreaPath.Split(';'); List <WBTerm> fterms = new List <WBTerm>(); foreach (string path in paths) { WBLogging.Migration.Verbose("Trying to get a Functional Area by path with: " + path); Term fterm = mapping.FunctionalAreasTaxonomy.GetOrCreateSelectedTermByPath(path); if (fterm != null) { fterms.Add(new WBTerm(mapping.FunctionalAreasTaxonomy, fterm)); } else { WBLogging.Debug("Coundn't find the functional area with path: " + path); } } if (fterms.Count > 0) { functionalArea = new WBTermCollection <WBTerm>(mapping.FunctionalAreasTaxonomy, fterms); } } string subjectTagsPaths = controlItem.WBxGetAsString(WBColumn.SubjectTagsPaths); if (String.IsNullOrEmpty(subjectTagsPaths) && mappingItem != null) { subjectTagsPaths = mappingItem.WBxGetAsString(WBColumn.SubjectTagsPaths); } if (!String.IsNullOrEmpty(subjectTagsPaths)) { List <WBSubjectTag> sterms = new List <WBSubjectTag>(); // Note that it is not necessarily an error for the subject tags to be empty. if (!String.IsNullOrEmpty(subjectTagsPaths) && subjectTagsPaths != "/") { string[] paths = subjectTagsPaths.Split(';'); foreach (string path in paths) { WBLogging.Migration.Verbose("Trying to get a Subject Tag by path with: " + path); if (path != "/") { Term sterm = mapping.SubjectTagsTaxonomy.GetOrCreateSelectedTermByPath(path); if (sterm != null) { sterms.Add(new WBSubjectTag(mapping.SubjectTagsTaxonomy, sterm)); } else { WBLogging.Debug("Coundn't find the subject tag with path: " + path); } } } } subjectTags = new WBTermCollection <WBSubjectTag>(mapping.SubjectTagsTaxonomy, sterms); } } else { recordsType = mappedPath.RecordsType; functionalArea = mappedPath.FunctionalArea; subjectTags = mappedPath.SubjectTags; } if (MigrationSubjectsList != null && MigrationSourceSystem == MIGRATION_SOURCE__HFI_INTRANET_DOCUMENTS) { //foreach (SPField field in migrationItem.Fields) //{ // WBLogging.Debug("Found field: " + field.Title + " field inner name: " + field.InternalName); //} subjectTags = AddAdditionalSubjectTags(controlSite, subjectTags, migrationItem.WBxGetAsString(WBColumn.SourceID)); } if (recordsType == null) { MigrationError(migrationItem, "The records type for this item could not be found. Looked for: " + mappedPath.RecordsTypePath); return; } if (functionalArea == null || functionalArea.Count == 0) { MigrationError(migrationItem, "The functional area for this item could not be found. Looked for: " + mappedPath.FunctionalAreaPath); return; } // OK so we can start building up our information about the document we are going to declare: WBDocument document = new WBDocument(); document.RecordsType = recordsType; document.FunctionalArea = functionalArea; document.SubjectTags = subjectTags; document[WBColumn.SourceFilePath] = sourceFilePath; string sourceSystem = migrationItem.WBxGetAsString(WBColumn.SourceSystem); if (String.IsNullOrEmpty(sourceSystem)) { sourceSystem = farm.MigrationSourceSystem; } if (String.IsNullOrEmpty(sourceSystem)) { sourceSystem = farm.MigrationControlListUrl; } document[WBColumn.SourceSystem] = sourceSystem; String sourceID = migrationItem.WBxGetAsString(WBColumn.SourceID); if (String.IsNullOrEmpty(sourceID) && MigrationSourceSystem != MIGRATION_SOURCE__DOCUMENTUM_WEB_DOCUMENTS) { sourceID = sourceFilePath; } document[WBColumn.SourceID] = sourceID; SPFile sourceFile = null; SPListItem sourceItem = null; if (MigrationSourceSystem == MIGRATION_SOURCE__DOCUMENTUM_WEB_DOCUMENTS) { if (String.IsNullOrEmpty(sourceID)) { sourceItem = sourceWeb.GetListItem(sourceFilePath); document[WBColumn.SourceID] = sourceFilePath; document[WBColumn.SourceSystem] = "Initial SharePoint Web Docs"; } else { sourceItem = WBUtils.FindItemByColumn(sourceSite, (SPList)sourceLibrary, WBColumn.Source_ID, sourceID); } if (sourceItem == null) { MigrationError(migrationItem, "Could not find the doc with source id = " + sourceFilePath); return; } sourceFile = sourceItem.File; } if (migrationItem.WBxIsNotBlank(WBColumn.ReferenceDateString)) { document.ReferenceDate = WBUtils.ParseDate(migrationItem.WBxGetAsString(WBColumn.ReferenceDateString)); } if (migrationItem.WBxIsNotBlank(WBColumn.ModifiedDateString)) { document.Modified = WBUtils.ParseDate(migrationItem.WBxGetAsString(WBColumn.ModifiedDateString)); } else { if (mappingItem != null) { if (mappingItem.WBxIsNotBlank(WBColumn.ModifiedDateString)) { document.Modified = WBUtils.ParseDate(mappingItem.WBxGetAsString(WBColumn.ModifiedDateString)); } } else if (subjectItem != null) { if (subjectItem.WBxIsNotBlank(WBColumn.ModifiedDateString)) { document.Modified = WBUtils.ParseDate(subjectItem.WBxGetAsString(WBColumn.ModifiedDateString)); } } else if (sourceItem != null) { if (sourceItem.WBxHasValue(WBColumn.Modified)) { document.Modified = (DateTime)sourceItem["Modified"]; } } } if (migrationItem.WBxIsNotBlank(WBColumn.DeclaredDateString)) { document.DeclaredRecord = WBUtils.ParseDate(migrationItem.WBxGetAsString(WBColumn.DeclaredDateString)); } if (migrationItem.WBxIsNotBlank(WBColumn.ScanDateString)) { document.ScanDate = WBUtils.ParseDate(migrationItem.WBxGetAsString(WBColumn.ScanDateString)); } if (migrationItem.WBxIsNotBlank(WBColumn.OwningTeamPath) || !String.IsNullOrEmpty(mappedPath.OwningTeamPath)) { WBTaxonomy teamsTaxonomy = mapping.TeamsTaxonomy; string owningTeamPath = migrationItem.WBxGetAsString(WBColumn.OwningTeamPath); if (owningTeamPath == "") { owningTeamPath = mappedPath.OwningTeamPath; } WBTeam foundTeam = teamsTaxonomy.GetSelectedTeam(WBUtils.NormalisePath(owningTeamPath)); if (foundTeam != null) { WBLogging.Migration.Verbose("Found the owning team: " + foundTeam.Name); document.OwningTeam = foundTeam; } else { MigrationError(migrationItem, "Could not find the owning team at: " + owningTeamPath); return; } } if (migrationItem.WBxIsNotBlank(WBColumn.Title)) { document[WBColumn.Title] = migrationItem.WBxGetAsString(WBColumn.Title); } if (MigrationSourceSystem == MIGRATION_SOURCE__HFI_INTRANET_DOCUMENTS) { document.Modified = File.GetLastWriteTime(sourceFilePath); WBLogging.Debug("Found the last modified date to be: " + document.Modified); } // We'll set the reference date for these imported files based on their existing declared date or modified date if it exists. if (!document.HasReferenceDate) { if (document.HasDeclaredRecord) { document.ReferenceDate = document.DeclaredRecord; } else if (document.HasScanDate) { document.ReferenceDate = document.ScanDate; } else if (document.HasModified) { document.ReferenceDate = document.Modified; } } if (migrationItem.WBxHasValue(WBColumn.ReferenceID)) { document.ReferenceID = migrationItem.WBxGetAsString(WBColumn.ReferenceID); } string protectiveZone = migrationItem.WBxGetAsString(WBColumn.ProtectiveZone); if (String.IsNullOrEmpty(protectiveZone)) { protectiveZone = mappedPath.ProtectiveZone; if (String.IsNullOrEmpty(protectiveZone)) { protectiveZone = WBRecordsType.PROTECTIVE_ZONE__PROTECTED; } } document[WBColumn.ProtectiveZone] = protectiveZone; string liveOrArchived = migrationItem.WBxGetAsString(WBColumn.LiveOrArchived); if (String.IsNullOrEmpty(liveOrArchived)) { liveOrArchived = mappedPath.LiveOrArchived; if (String.IsNullOrEmpty(liveOrArchived)) { liveOrArchived = WBColumn.LIVE_OR_ARCHIVED__LIVE; } } document[WBColumn.LiveOrArchived] = liveOrArchived; bool downloadFromWebSite = false; if (MigrationSourceSystem == MIGRATION_SOURCE__ALFRESCO_RECORDS) { downloadFromWebSite = true; } String originalFileName = migrationItem.WBxGetAsString(WBColumn.OriginalFilename).Trim(); if (String.IsNullOrEmpty(originalFileName)) { originalFileName = Path.GetFileName(sourceFilePath); } if (MigrationSourceSystem == MIGRATION_SOURCE__DOCUMENTUM_WEB_DOCUMENTS) { originalFileName = sourceFile.Name; } if (downloadFromWebSite) { originalFileName = HttpUtility.UrlDecode(originalFileName); } document.OriginalFilename = originalFileName; //String extension = Path.GetExtension(filename); WBItemMessages metadataErrors = recordsType.CheckMetadataIsOK(document); if (metadataErrors.Count > 0) { string message = "There were problems with the prepared metadata. "; foreach (WBColumn column in metadataErrors.Keys) { message += "Error for column: " + column.DisplayName + " message: " + metadataErrors[column] + " "; } MigrationError(migrationItem, message); return; } Stream fileStream = null; if (downloadFromWebSite) { WebClient webClient = new WebClient(); if (!String.IsNullOrEmpty(farm.MigrationUserName) && !String.IsNullOrEmpty(farm.MigrationPassword)) { webClient.Credentials = new NetworkCredential(farm.MigrationUserName, farm.MigrationPassword); } string tempFile = @"C:\Temp\tmp.bin"; if (farm.FarmInstance == WBFarm.FARM_INSTANCE__PROTECTED_INTERNAL_FARM) { tempFile = @"E:\Temp\tmp.bin"; } webClient.DownloadFile(sourceFilePath, tempFile); WBLogging.Migration.Verbose("Downloaded to local tmp file using webClient.DownloadFile() successfully"); fileStream = File.OpenRead(tempFile); WBLogging.Migration.Verbose("Opened local tmp file using File.OpenRead() successfully"); } else if (MigrationSourceSystem == MIGRATION_SOURCE__DOCUMENTUM_WEB_DOCUMENTS) { fileStream = sourceFile.OpenBinaryStream(); WBLogging.Migration.Verbose("Opened using sourceFile.OpenBinaryStream() successfully"); } else { fileStream = File.OpenRead(sourceFilePath); WBLogging.Migration.Verbose("Opened using File.OpenRead() successfully"); } SPListItem uploadedItem = null; try { uploadedItem = recordsType.PublishDocument(destinationWeb, destinationRootFolder, document, fileStream); } finally { fileStream.Close(); fileStream.Dispose(); } if (uploadedItem == null) { MigrationError(migrationItem, "There was a problem in the call to recordsType.PublishDocument() as the uploaded item is null."); return; } migrationItem.WBxSet(WBColumn.DateMigrated, DateTime.Now); migrationItem.WBxSet(WBColumn.MigratedToUrl, uploadedItem.WBxGet(WBColumn.EncodedAbsoluteURL)); migrationItem.WBxSet(WBColumn.RecordID, uploadedItem.WBxGet(WBColumn.RecordID)); migrationItem.WBxSet(WBColumn.MigrationStatus, WBColumn.MIGRATION_STATUS__DONE); migrationItem.Update(); }