private void DeployTermUnderTermSet(object modelHost, TermSetModelHost groupModelHost, TaxonomyTermDefinition termModel)
        {
            var termStore = groupModelHost.HostTermStore;
            var termSet   = groupModelHost.HostTermSet;

            var currentTerm = FindTermInTermSet(termSet, termModel);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = currentTerm,
                ObjectType       = typeof(Term),
                ObjectDefinition = termModel,
                ModelHost        = modelHost
            });

            if (currentTerm == null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new Term");

                currentTerm = termModel.Id.HasValue
                    ? termSet.CreateTerm(termModel.Name, termModel.LCID, termModel.Id.Value)
                    : termSet.CreateTerm(termModel.Name, termModel.LCID, Guid.NewGuid());

                MapTermProperties(currentTerm, termModel);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioned,
                    Object           = currentTerm,
                    ObjectType       = typeof(Term),
                    ObjectDefinition = termModel,
                    ModelHost        = modelHost
                });
            }
            else
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing Term");

                MapTermProperties(currentTerm, termModel);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioned,
                    Object           = currentTerm,
                    ObjectType       = typeof(Term),
                    ObjectDefinition = termModel,
                    ModelHost        = modelHost
                });
            }

            termStore.CommitAll();
            termStore.Context.ExecuteQueryWithTrace();
        }
Beispiel #2
0
        private void DeployTermUnderTermSet(object modelHost, TermSetModelHost groupModelHost, TaxonomyTermDefinition termModel)
        {
            var termStore = groupModelHost.HostTermStore;
            var termSet   = groupModelHost.HostTermSet;

            var currentTerm = FindTermInTermSet(termSet, termModel);
            var termName    = NormalizeTermName(termModel.Name);



            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = currentTerm,
                ObjectType       = typeof(Term),
                ObjectDefinition = termModel,
                ModelHost        = modelHost
            });

            if (currentTerm == null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new Term");

                currentTerm = termModel.Id.HasValue
                    ? termSet.CreateTerm(termName, termModel.LCID, termModel.Id.Value)
                    : termSet.CreateTerm(termName, termModel.LCID, Guid.NewGuid());

                MapTermProperties(currentTerm, termModel, true);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioned,
                    Object           = currentTerm,
                    ObjectType       = typeof(Term),
                    ObjectDefinition = termModel,
                    ModelHost        = modelHost
                });
            }
            else
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing Term");

                MapTermProperties(currentTerm, termModel, false);

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model            = null,
                    EventType        = ModelEventType.OnProvisioned,
                    Object           = currentTerm,
                    ObjectType       = typeof(Term),
                    ObjectDefinition = termModel,
                    ModelHost        = modelHost
                });
            }

            try
            {
                termStore.CommitAll();
                currentTerm.RefreshLoad();

                termStore.Context.ExecuteQueryWithTrace();
            }
            catch (Exception e)
            {
                var context = groupModelHost.HostClientContext;

                if (!IsSharePointOnlineContext(context))
                {
                    throw;
                }

                var serverException = e as ServerException;

                if (serverException != null &&
                    ReflectionUtils.GetHResultValue(serverException).Value == -2146233088 &&
                    serverException.ServerErrorTypeName == "Microsoft.SharePoint.Taxonomy.TermStoreOperationException")
                {
                    TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing Term");

                    currentTerm = FindTermInTermSet(termSet, termModel);

                    if (currentTerm == null)
                    {
                        TryRetryService.TryWithRetry(() =>
                        {
                            currentTerm = FindTermInTermSet(termSet, termModel);
                            return(currentTerm != null);
                        });
                    }

                    MapTermProperties(currentTerm, termModel, false);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model            = null,
                        EventType        = ModelEventType.OnProvisioned,
                        Object           = currentTerm,
                        ObjectType       = typeof(Term),
                        ObjectDefinition = termModel,
                        ModelHost        = modelHost
                    });

                    termStore.CommitAll();
                    termStore.Context.ExecuteQueryWithTrace();
                }
            }
        }