How to check if sharepoint group exists
in  Visual Studio 2013 workflows


Posted on September 23rd, 2014

In this post, you will learn how to check if SharePoint group exists while working with Visual Studio 2013 workflows. We will be using ResponseStatuscode property of HttpSend activity to find out whether the SharePoint group exists or not.

Please check our previous blog post,  step1 and step 2 on how to create a list workflow. We are also reusing the workflow configuration to get app web URL from our previous blog posts.

We are sharing on workflows in SharePoint 2013 and Office 365 using Visual Studio 2013. These apply to both on-premises and online. All these blog posts based on SharePoint hosted apps.

Let us check what are the workflow activities required to check if SharePoint group exists or not.

Workflow Activities:

In this workflow, we will use below workflow activities to check if SharePoint group exists in workflows using Visual Studio 2013.

  • LookupSPListItem
  • GetDynamicValueProperties
  • Assign
  • WriteToHistory

We have explained more details about above activities in previous posts. Please refer previous posts to get more details about these activities.

  • HttpSend
  • GetDynamicValueProperty<T>

Completed workflow:

The complete workflow I have to divide into two parts. The First image shows how to get the App we URL and the group name declaration. Name of the SharePoint group is Product Owners. Next image is to check if the group exists or not.

The below image is the continuation of the above image.(Apologies, I could not able to capture everything into single image)

Workflow Variables:

Below is the list of variables we have used in this workflow.

Workflow Activities to Check if SharePoint Group exists or not:

Step1:Assign:(Building Rest URI)

In step 1, we are building the Rest Uri for getting group details based on the name.

You can see that we are reusing the App web URL and appending the group name.

RestApi = string.Format("{0}_api/web/sitegroups/getbyname('" + GroupName + "')", Appweburl)

If you don't break the permissions at the app, you can use the host web URL also and format the above RestApi string variable because app web also uses the same SharePoint groups available at host web.

To learn more about Building workflows, click the image below and enroll in the FREE course.

Step 2.HttpSend:(Rest Call)

We are using HttpSend activity to make Rest call, which is using "GET" method in this example.

You can see in the below screen, what are the minimum properties need to be set for making Rest call.

The response of this Rest call will be available in "ProductOwnersgroupResonse" DynamicValue variable.

"Statuscode" variable will be used to store the response code. We will use this to find out whether the SharePoint group is available or not.

For the Request headers, we are expecting Json response, so we have set both the Accept and Content-Type headers to "application/json;odata=verbose".

Step 3.If condition(Checking Response Status code)

If the HttpSend activity  "ResponseStatusCode" property returns "OK" then the call was successful. In Step 3, we are checking whether the code return OK or some other value. If it is "OK" then we are logging into workflow history list saying that SharePoint group is available.

Below is the list of possible values of httpstatuscode http://msdn.microsoft.com/en-us/library/system.net.httpstatuscode.aspx.


Step 4.GetDynamicValueProperty<T>(Get Error message)

In Step 4, if the ResponseStatusCode value is different from "OK", then we are using 

GetDynamicValueProperty<Ststem.string> to capture the error message.

While we are testing the query using postman tool, it will show the below response.

Based on the out put from the tool, I have added the property name "error/message/value" from the "ProductOwnersgroupResonse" which is from HttpSend activity.

Once you complete the workflow with the all the steps, let's start testing.

Testing:

Right-click on the project and click on deploy. Once we deploy it, add an item in the Products list, and it should start the workflow automatically. Now go to workflow status page to check the workflow history Info. If the Product Owners SharePoint Group doesn't exist then, we will get below message.

If we have Product Owners SharePoint Group, then we will get below message in the workflow history information.

Code Repository:

You can download the code for this demo by clicking on the image below.

Conclusion:

In this post, you have learned how to check if SharePoint group exists in  Visual Studio 2013 workflows.

To learn more about Building workflows, click the image below and enroll in the FREE course.

› How to check if sharepoint group exists in Visual Studio 2013 workflows

About Author

Devendra Velegandla is a four-time recipient of Microsoft's Most Valuable Professional (MVP) award (2014-2017) for Office Servers and Services MVP and  SharePoint Server. He received the MCC award for his contributions in Microsoft MSDN/TechNet forums. He loves sharing articles, videos, and tutorial on SharePoint and Office 365.


If you found this post helpful, please share it with your friends!!


Join 6,000 subscribers and receive instant access to

Devendra's Library of Resources for SharePoint Developers