Skip to main content

SharePoint Nintex Workflow Custom Actions developments using server side object model

Read FromProperty Activity
-
------------------------------------------------------------------------------------------------------------------------

using System;
using System.Workflow.ComponentModel;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using Nintex.Workflow;
using Microsoft.SharePoint;

namespace CFSP.CustomActions.ReadFromPropertyBag
{
    public class ReadFromPropertyBagActivity : Nintex.Workflow.Activities.ProgressTrackingActivity
    {
        public static DependencyProperty __ListItemProperty = DependencyProperty.Register("__ListItem", typeof (SPItemKey), typeof (ReadFromPropertyBagActivity));
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof (WorkflowContext), typeof (ReadFromPropertyBagActivity));
        public static DependencyProperty __ListIdProperty = DependencyProperty.Register("__ListId", typeof (string),typeof (ReadFromPropertyBagActivity));
        public static DependencyProperty WebProperty = DependencyProperty.Register("Web", typeof(string), typeof(ReadFromPropertyBagActivity));
        public static DependencyProperty PropertyProperty = DependencyProperty.Register("Property", typeof (string), typeof (ReadFromPropertyBagActivity));
        public static DependencyProperty ResultOutputProperty = DependencyProperty.Register("ResultOutput", typeof (string), typeof (ReadFromPropertyBagActivity));

        public WorkflowContext __Context
        {
            get { return (WorkflowContext) base.GetValue(__ContextProperty); }
            set { base.SetValue(__ContextProperty, value); }
        }

        public SPItemKey __ListItem
        {
            get { return (SPItemKey) base.GetValue(__ListItemProperty); }
            set { base.SetValue(__ListItemProperty, value); }
        }

        public string __ListId
        {
            get { return (string) base.GetValue(__ListIdProperty); }
            set { base.SetValue(__ListIdProperty, value);}
        }

        public string Web
        {
            get { return (string) base.GetValue(WebProperty); }
            set { base.SetValue(WebProperty, value);}
        }

        public string Property
        {
            get { return (string) base.GetValue(PropertyProperty); }
            set { base.SetValue(PropertyProperty, value);}
        }

        public string ResultOutput
        {
            get { return (string) base.GetValue(ResultOutputProperty); }
            set {base.SetValue(ResultOutputProperty, value);}
        }

        public ReadFromPropertyBagActivity()
        {

        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            //Standard Nintex code to obtain context.
            ActivityActivationReference.IsAllowed(this, __Context.Web);
            NWWorkflowContext ctx = NWWorkflowContext.GetContext(
               this.__Context,
               new Guid(this.__ListId),
               this.__ListItem.Id,
               this.WorkflowInstanceId,
               this);
            base.LogProgressStart(ctx);

            //Get the property value.
            string resolvedProperty = ctx.AddContextDataToString(this.Property);

            var result = "";

            //Using the context get the property if it exists.
            if (ctx.Web.AllProperties.ContainsKey(resolvedProperty))
            {
                result = ctx.Web.AllProperties[resolvedProperty].ToString();
            }
         
            //store the result.
            this.ResultOutput = result;
            //End Execution.
            base.LogProgressEnd(ctx, executionContext);
            return ActivityExecutionStatus.Closed;
        }

        protected override ActivityExecutionStatus HandleFault(ActivityExecutionContext executionContext, Exception exception)
        {
            Nintex.Workflow.Diagnostics.ActivityErrorHandler.HandleFault(executionContext, exception,
                this.WorkflowInstanceId, "Error Reading from Property Bag", __ListItem.Id, __ListId, __Context);

            return base.HandleFault(executionContext, exception);
        }
    }
}

Read FromProperty Adapter
-----------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Workflow.ComponentModel;
using Microsoft.SharePoint;
using Nintex.Workflow;
using Nintex.Workflow.Activities.Adapters;

namespace CFSP.CustomActions.ReadFromPropertyBag
{
    public class ReadFromPropertyBagAdapter : GenericRenderingAction
    {

        private const string PropertyProperty = "Property";
        private const string ResultOutputProperty = "ResultOutput";

        public override NWActionConfig GetDefaultConfig(GetDefaultConfigContext context)
        {
            NWActionConfig config = new NWActionConfig(this);

            config.Parameters = new ActivityParameter[2];

            //define the parameters that the user can configure for this action.
            config.Parameters[0] = new ActivityParameter();
            config.Parameters[0].Name = PropertyProperty;
            config.Parameters[0].PrimitiveValue = new PrimitiveValue();
            config.Parameters[0].PrimitiveValue.Value = string.Empty;
            config.Parameters[0].PrimitiveValue.ValueType = SPFieldType.Text.ToString();

            config.Parameters[1] = new ActivityParameter();
            config.Parameters[1].Name = ResultOutputProperty;
            config.Parameters[1].Variable = new NWWorkflowVariable();

            //set the default label for the action.
            config.TLabel = ActivityReferenceCollection.FindByAdapter(this).Name;
            return config;

        }

        public override bool ValidateConfig(ActivityContext context)
        {
            //Add logic to validate the configuration here.
            bool isValid = true;

            Dictionary<string, ActivityParameterHelper> parameters = context.Configuration.GetParameterHelpers();

            if (!parameters[PropertyProperty].Validate(typeof(string), context))
            {
                isValid &= false;
                validationSummary.AddError("Property Bag", ValidationSummaryErrorType.CannotBeBlank);
            }
            return isValid;
        }

        public override CompositeActivity AddActivityToWorkflow(PublishContext context)
        {
            //Create an instance of the Activity and set its properties based on config. Add it to the parent activity
            Dictionary<string, ActivityParameterHelper> parameters = context.Config.GetParameterHelpers();

            ReadFromPropertyBagActivity activity = new ReadFromPropertyBagActivity();
            parameters[PropertyProperty].AssignTo(activity, ReadFromPropertyBagActivity.PropertyProperty, context);
            parameters[ResultOutputProperty].AssignTo(activity, ReadFromPropertyBagActivity.ResultOutputProperty, context);

            activity.SetBinding(ReadFromPropertyBagActivity.__ContextProperty, new ActivityBind(context.ParentWorkflow.Name, StandardWorkflowDataItems.__context));
            activity.SetBinding(ReadFromPropertyBagActivity.__ListItemProperty, new ActivityBind(context.ParentWorkflow.Name, StandardWorkflowDataItems.__item));
            activity.SetBinding(ReadFromPropertyBagActivity.__ListIdProperty, new ActivityBind(context.ParentWorkflow.Name, StandardWorkflowDataItems.__list));

            ActivityFlags f = new ActivityFlags();
            f.AddLabelsFromConfig(context);
            f.AssignTo(activity);

            context.ParentActivity.Activities.Add(activity);
            return null;
        }

        public override NWActionConfig GetConfig(RetrieveConfigContext context)
        {
            //Read the properties from the context.ACtivity and update the values in the NWActionConfig
            NWActionConfig config = this.GetDefaultConfig(context);

            Dictionary<string, ActivityParameterHelper> parameters = config.GetParameterHelpers();
            parameters[PropertyProperty].RetrieveValue(context.Activity, ReadFromPropertyBagActivity.PropertyProperty, context);
            parameters[ResultOutputProperty].RetrieveValue(context.Activity, ReadFromPropertyBagActivity.ResultOutputProperty, context);

            return config;
        }

        public override ActionSummary BuildSummary(ActivityContext context)
        {
            // Construct an ActionSummary class to display details about this action.
            Dictionary<string, ActivityParameterHelper> parameters = context.Configuration.GetParameterHelpers();
            return new ActionSummary("Retrieve the following Property bag: {0}", parameters[PropertyProperty].Value);
        }
    }
}




Comments

Popular posts from this blog

Update user language and regional settings with CSOM

Following my previous post around multilingual aspects of SharePoint Online:  Modify Site Regional and Language settings with JSOM and JavaScript Here is some CSOM code which updates the personal regional settings of the current user or another user (if you are a tenant admin and have the rights to update user profiles) Before update: The code: using Microsoft.SharePoint.Client ; using Microsoft.SharePoint.Client.UserProfiles ; using System.Security ; namespace UpdateLanguagePreference { class Program { static void Main ( string[] args ) { //Tenant Admin Details string tenantAdministrationUrl = " https://yoursite-admin.sharepoint.com/ " ; string tenantAdminLoginName = " tenantadmin@yoursite.onmicrosoft.com " ; string tenantAdminPassword = " password " ; //AccountName of the user whos property you want to update. ...

Power shell Script to get External Users on Office 365

Hello Everyone,  Below is the Powershell script to get all external users from office 365 tenant. #--------------------------------------------------------------------------------------------------------------------------- $host.Runspace.ThreadOptions = "ReuseThread" #Definition of the function that gets all the external users in a SharePoint Online Tenant. function Get-SPOExternalUsers {     param ($sUserName,$sMessage,$sSPOAdminCenterUrl)     try     {             Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green         Write-Host "Getting all the external users in a SharePoint Online Tenant" -foregroundcolor Green         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green         $msolcred = Get-Credential -UserName...

Build and Deploy a custom theme in SharePoint 2019: Using C# and Site features

Custom Theme and Branding are common to use cases and all kinds of business users like to have the personalized team and collaboration sites. today I am posting C# code that will help to build custom features for SharePoint 2019 site.   If need any assistance, I would be happy to help: kamal_pandey@outlook.com  ----------------------------- Code to build  custom features---------- using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; namespace SharePointBranding.Code {     public class BrandingManager     {         public SPWeb Web { get; set; }         public BrandingManager(SPWeb web)         {             Web = web;         }         public void SetMasterPage(stri...