Programmatically retrieve logged in user in SharePoint 2013 BCS Object Model with Visual Studio 2012

Ashok Raja
 
Solutions Architect
February 5, 2013
 
Rate this article
 
Views
807

In this article we can see how to pass current logged in user to BCS Object Model and retrieve his login ID programmatically in SharePoint 2013. This would help to filter and display external data based on logged in user account.

To begin with I have created an empty SharePoint 2013 project and have added a BDC model to it. The objective is to pass the logged in user’s Login ID as a parameter for ReadList method. To achieve this I created a new “In” parameter for the ReadList and mapped it with the newly created filter descriptor with the type UserContext. This adds a new parameter to the ReadList method and passes the logged in user’s login Id on execution via this parameter.

Let’s see how to configure bdcm diagram step by step.

1. Double click the bdcm file, which will open up the Visual designer

2. Click the BDC Entity diagram, which will enable the BDC Method Details Window. If you are not able to view that window, select it from View=>Other Windows => BDC Method Details from Visual Studio Menu

3. Select ReadList under Methods section and navigate to parameters group. Click “Add a Parameter” Link and add a parameter named as “Current User”

4. Navigate to “Filter Descriptors” section and add a new filter descriptor by name “CurrentUserFilterDescriptor

clip_image002[4]

5. Invoke the properties window by pressing the “F4” key

6. Change the “Type” property to “UserContext

7. Change the “Associated Type Descriptor” property to “CurrentUserTypeDescriptor

clip_image003[4]

8. Save the changes and open up Entity1Service.cs file

9. If you have a look into ReadList Method, the method signature might have changed automatically. “CurrentUser” is added as a parameter to ReadList due to the action that we have performed in Step 3

10. Make a change to the entity1.Message to include “CurrentUser” to the output string. In the real world scenario, value from this parameter can be used to filter data from web service or any other external data requests based on logged in user. For this demonstration , I have changed the code as mentioned below

clip_image004[4]

11. Now we are ready to go, build and deploy the application. ( Consider the below section if you are new to BCS with Visual Studio )

Things to consider for a BDC Project developed in Visual Studio

Since it’s a BDC project, please take care the below points, that are common to any BDC application developed in Visual Studio.

1. Ensure that the associated feature is a Farm Feature

2. Assign “SiteUrl” property to BDC feature. (Any existing SharePoint web application URL in current farm can be used. Still this step is required in SharePoint 2013 as what we have done for SharePoint 2010).

To Assign Site URL, double click the feature which contains this bdcm file and select the “Manifest” Tab and scroll down to the bottom of the window and click “Edit Options

 <?xml version="1.0" encoding="utf-8" ?>
 <Feature xmlns="http://schemas.microsoft.com/sharepoint/">
     <Properties>
         <Property Key="SiteUrl" Value="[Replace it with a SharePoint web application url]" />
     </Properties>
 </Feature>

clip_image005[4]

Save the Feature and close it.

3. After deployment, log in into Central Administration and set appropriate permissions to the deployed BDC object.

Follow the below steps to set the permission to BDC model

a. Log In into Central Administration Web Application

b. Click “Manage Service Applications” located under “Application Management

clip_image006[4]

c. Select “Business Connectivity Service Application

clip_image008[4]

d. Select the BDC object that have been deployed and click the “Set Permissions” link in the context menu.

clip_image009[4]

e. Set the permission for appropriate groups. For this demo purpose I have added Administrator with full rights and All Authenticated Users with Execution rights.

clip_image010[4]

f. Repeat this process for “BDC Models”, “External Systems” and “External Content Types” categories. That’s it for setting the permission.

clip_image011[4]

External List

After deployment, open a SharePoint 2013 site and create a new external list based on the currently deployed BDC Object. If you have followed all the steps properly as mentioned above, you can expect the final output as displayed below.

Note: If you are trying in a SharePoint 2013 Publishing Site, you have to activate “Team Collaboration Lists” feature to enable listing of “External List App”

clip_image012[4]

Note: If you are interested to know, how to retrieve User Name and Password stored in Secure Store Service via SharePoint Object model in BCS check out this article.

If you would like to assign custom properties during BDC design and retrieve those properties via SharePoint Object Model during execution, check out this article.

Author Info

Ashok Raja
 
Solutions Architect
 
Rate this article
 
I am Ashok Raja, Share Point Consultant and Architect based out of Chennai, India. ...read more
 

Leave a comment