/// <summary>
        /// Imports the new SiteUsers to the target SharePoint.
        /// </summary>
        /// <exception cref="ElementsMigrationException">If the migration fails</exception>
        private void ImportNewUsers()
        {
            Console.WriteLine("import new Users...");
            Logger.AddMessage("import new Users...");
            UserCollection sourceUserCollection = this.GetAllUser(SourceClientContext);
            UserCollection targetUserCollection = this.GetAllUser(TargetClientContext);

            HashSet <string> targetUserNames = targetUserCollection.GetAllLoginNames();

            foreach (var sourceUser in sourceUserCollection)
            {
                if (!targetUserNames.Contains(sourceUser.LoginName))
                {
                    Console.WriteLine("Import user '{0}'", sourceUser.LoginName);
                    Logger.AddMessage("import User '" + sourceUser.LoginName + "'");

                    UserCreationInformation creationObject = new UserCreationInformation();
                    creationObject.Email     = sourceUser.Email;
                    creationObject.LoginName = sourceUser.LoginName;
                    creationObject.Title     = sourceUser.Title;

                    User targetUser = targetUserCollection.Add(creationObject);

                    try
                    {
                        targetUser.IsSiteAdmin = sourceUser.IsSiteAdmin;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }

                    try
                    {
                        targetUser.Tag = sourceUser.Tag;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }
                }
                else
                {
                    Console.WriteLine("user '{0}' is already on target server. nothing to import.", sourceUser.LoginName);
                    Logger.AddMessage("don't have to import user '" + sourceUser.LoginName + "'");
                }
            }

            try
            {
                TargetClientContext.ExecuteQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception during importing the SiteUsers.", e);
                Logger.AddMessage("Exception during importing the Users. Error = " + e.Message);
                throw new ElementsMigrationException("Exception during importing the SiteUsers.", e);
            }
        }
Exemple #2
0
        /// <summary>
        /// Imports the new RoleDefinitions.
        /// </summary>
        /// <exception cref="ElementsMigrationException">If migration fails</exception>
        private void ImportNewRoleDefinitions()
        {
            Console.WriteLine("import new RoleDefinitions...");
            Logger.AddMessage("import new RoleDefinitions...");
            RoleDefinitionCollection sourceRoleDefinitionCollection = this.GetAllRollDefinitions(SourceClientContext);
            RoleDefinitionCollection targetRoleDefinitionCollection = this.GetAllRollDefinitions(TargetClientContext);

            HashSet <string> targetRoleDefinitionNames = targetRoleDefinitionCollection.ReadNames();

            foreach (var sourceRoleDefinition in sourceRoleDefinitionCollection)
            {
                if (!targetRoleDefinitionNames.Contains(sourceRoleDefinition.Name))
                {
                    Console.WriteLine("import roleDefinition '{0}'", sourceRoleDefinition.Name);
                    Logger.AddMessage("import RoleDefinition '" + sourceRoleDefinition.Name + "'");

                    RoleDefinitionCreationInformation creationObject = new RoleDefinitionCreationInformation();
                    creationObject.BasePermissions = sourceRoleDefinition.BasePermissions;
                    creationObject.Description     = sourceRoleDefinition.Description;
                    creationObject.Name            = sourceRoleDefinition.Name;
                    creationObject.Order           = sourceRoleDefinition.Order;

                    RoleDefinition targetRoleDefinition = targetRoleDefinitionCollection.Add(creationObject);
                    try
                    {
                        targetRoleDefinition.Tag = sourceRoleDefinition.Tag;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }
                }
                else
                {
                    Console.WriteLine("don't have to import '{0}'", sourceRoleDefinition.Name);
                    Logger.AddMessage("don't have to import '" + sourceRoleDefinition.Name + "'");
                }
            }

            try
            {
                TargetClientContext.ExecuteQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception during importing new RoleDefinition.", e);
                Logger.AddMessage("Exception during importing new RoleDefinition. Error = " + e.Message);
                throw new ElementsMigrationException("Exception during importing new RoleDefinition.", e);
            }
        }
Exemple #3
0
        /// <summary>
        /// Gets the WorkflowTemplate
        /// </summary>
        /// <returns>the workflowTemplate</returns>
        /// <exception cref="ElementsMigrationException">if there is no workflowTemplate</exception>
        private WorkflowTemplate GetTemplate()
        {
            WorkflowTemplateCollection sourceWorkflowTemplateCollection = SourceClientContext.Web.WorkflowTemplates;
            WorkflowTemplateCollection targetWorkflowTemplateCollection = TargetClientContext.Web.WorkflowTemplates;

            try
            {
                SourceClientContext.ExecuteQuery();
                TargetClientContext.ExecuteQuery();

                if (targetWorkflowTemplateCollection == null || targetWorkflowTemplateCollection.Count == 0)
                {
                    Logger.AddMessage("No templates for Workflow found!");
                    throw new ElementsMigrationException("No templates for Workflow found!");
                }

                if (sourceWorkflowTemplateCollection == null || sourceWorkflowTemplateCollection.Count == 0)
                {
                    //// nothing to search return target->first
                    return(targetWorkflowTemplateCollection.First());
                }

                foreach (var targetWorkflowTemplate in targetWorkflowTemplateCollection)
                {
                    foreach (var sourceWorkflowTemplate in sourceWorkflowTemplateCollection)
                    {
                        if (targetWorkflowTemplate.Name == sourceWorkflowTemplate.Name)
                        {
                            return(targetWorkflowTemplate);
                        }
                    }
                }

                Logger.AddMessage("No templates for Workflow found!");
                throw new ElementsMigrationException("No templates for Workflow found!");
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception during fetching the template of the Workflow.", e);
                Logger.AddMessage("Exception during fetching the template of the Workflow. Error = " + e.Message);

                throw new ElementsMigrationException("Exception during fetching the WorkflowTemplates.", e);
            }
        }
Exemple #4
0
        /// <summary>
        /// Imports the new ContentTypes.
        /// </summary>
        /// <exception cref="ElementsMigrationException">If migration fails</exception>
        private void ImportNewContentTypes()
        {
            Console.WriteLine("import new ContentTypes...");
            Logger.AddMessage("import new ContentTypes...");

            ContentTypeCollection contentTypeCollectionSourceServer = this.GetAllContentTypes(SourceClientContext);
            ContentTypeCollection contentTypeCollectionTargetServer = this.GetAllContentTypes(TargetClientContext);

            HashSet <string> namesOfContentTypesOnTargetServer = contentTypeCollectionTargetServer.GetNames();

            foreach (var contentType in contentTypeCollectionSourceServer)
            {
                if (!namesOfContentTypesOnTargetServer.Contains(contentType.Name))
                {
                    Console.WriteLine("import contentType = {0}", contentType.Name);
                    Logger.AddMessage("import ContentType = '" + contentType.Name + "'");

                    this.CreateContentType(contentTypeCollectionSourceServer, contentTypeCollectionTargetServer, contentType);
                }
                else
                {
                    Console.WriteLine("don't have to migrate '{0}'", contentType.Name);
                    Logger.AddMessage("Don't have to migrate '" + contentType.Name + "'");
                }
            }

            try
            {
                TargetClientContext.ExecuteQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception during importing new ContentTypes.", e);
                Logger.AddMessage("Exception during importing new ContentTypes. Error = " + e.Message);
                throw new ElementsMigrationException("Exception during importing new ContentTypes.", e);
            }
        }
        /// <summary>
        /// Import new fields to the target SharePoint.
        /// </summary>
        /// <exception cref="ElementsMigrationException">if importing of fields fails</exception>
        private void ImportNewField()
        {
            Console.WriteLine("import new fields");
            Logger.AddMessage("import new SiteColumns");
            FieldCollection sourceFieldCollection = this.GetAllFields(SourceClientContext);
            FieldCollection targetFieldCollection = this.GetAllFields(TargetClientContext);

            HashSet <string> targetFieldTitles = targetFieldCollection.GetAllTitles();

            foreach (var sourceField in sourceFieldCollection)
            {
                if (!targetFieldTitles.Contains(sourceField.Title))
                {
                    Logger.AddMessage("import new field = '" + sourceField.Title + "'");
                    string newField    = "<Field DisplayName='" + sourceField.Title + "' Type='" + sourceField.TypeAsString + "' />";
                    Field  targetField = targetFieldCollection.AddFieldAsXml(newField, true, AddFieldOptions.DefaultValue);
                    targetField.Description         = sourceField.Description;
                    targetField.Direction           = sourceField.Direction;
                    targetField.EnforceUniqueValues = sourceField.EnforceUniqueValues;
                    targetField.FieldTypeKind       = sourceField.FieldTypeKind;
                    //// TODO getGroup: targetField.Group = sourceField.Group;
                    targetField.Hidden = sourceField.Hidden;
                    try
                    {
                        targetField.Indexed = sourceField.Indexed;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }

                    try
                    {
                        targetField.ReadOnlyField = sourceField.ReadOnlyField;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }

                    try
                    {
                        targetField.Required = sourceField.Required;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }

                    try
                    {
                        targetField.StaticName = sourceField.StaticName;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }

                    try
                    {
                        targetField.Tag = sourceField.Tag;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }

                    try
                    {
                        targetField.TypeAsString = sourceField.TypeAsString;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }

                    try
                    {
                        targetField.ValidationFormula = sourceField.ValidationFormula;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }

                    try
                    {
                        targetField.ValidationMessage = sourceField.ValidationMessage;
                    }
                    catch (PropertyOrFieldNotInitializedException)
                    {
                    }
                }
                else
                {
                    Console.WriteLine("don't have to import '{0}'", sourceField.Title);
                    Logger.AddMessage("don't have to import '" + sourceField.Title + "'");
                }
            }

            try
            {
                TargetClientContext.ExecuteQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception during importing new Fields.", e);
                Logger.AddMessage("Exception during importing new Fields. Error = " + e.Message);
                throw new ElementsMigrationException("Exception during importing new Fields.", e);
            }
        }