Check current user belong to particular group using ECMAScript

The following ECMAScript checks, whether the current logged in user belongs to particular group.

In my case there was a scenario, where administrator was able to see all fields on form, while current user was able to see limited set of fields.

Add this script on that page or create JS file and refer in page, where you want to check, if current user is in particular group

<script type=”text/javascript”>

//this make sure the function is called once the page is loaded completely

_spBodyOnLoadFunctionNames.push(onPageLoad());

function onPageLoad() {

ExecuteOrDelayUntilScriptLoaded(checkUser, “sp.js”)

}
var adminUsers;

var vCurrUserName;

function checkUser() {

var currentContext = new SP.ClientContext();

//get current user

vCurrUserName = currentContext.get_web().get_currentUser();

currentContext.load(vCurrUserName);

// get Admin group

var groupCollection = currentContext.get_web().get_siteGroups();

var _group = groupCollection.getById(10); // ID of the Group

adminUsers = _group.get_users();

currentContext.load(adminUsers);

currentContext.executeQueryAsync(Function.createDelegate(this, this.checkUserSuccess), Function.createDelegate(this, this.checkUserFailure));

}

function checkUserSuccess() {

var vIsAdmin = false;

var listEnumerator = adminUsers.getEnumerator();

while (listEnumerator.moveNext()) {

var item = listEnumerator.get_current();

//check current user login name and user login name from admin group

if (vCurrUserName.get_loginName() == item.get_loginName()) {

vIsAdmin = true;

break;

}

}

if (vIsAdmin) {

//Write code to show fields

if (document.getElementById(‘CheckBoxColID’) != null) {

document.getElementById(‘CheckBoxColID’).style.display = “block”;

}

}

else {

//Write code to hide fields

if (document.getElementById(‘CheckBoxColID’) != null) {

document.getElementById(‘CheckBoxColID’).style.display = “none”;

}

}

}

function checkUserFailure() {

//Failed to get user. This function will execute only if the user doesn’t belong to admin group or doesn’t have permission to view members from admin group

//Write code to hide fields

if (document.getElementById(‘CheckBoxColID’) != null) {

document.getElementById(‘CheckBoxColID’).style.display = “none”;

}

}
</script>

Filter Data View Web Part By Date And Time

Filter Data View Web Part by Date & Time

In the previous blog Announcement WebPart, default filter of Data View WebPart  is used.

Data View Web Part filters the Date & Time column by Date only.

To filter the Date column by time

Go to the Filter option

Tick the Check Box Add XSLT Filtering. Click on Edit

Add following condition

number(translate(ddwrt:FormatDateTime(string(@Expires),1033,’yyyyMMdd HHmm’),’ ‘,”))-number(translate(ddwrt:FormatDateTime(string(ddwrt:TodayIso()),1033,’yyyyMMdd HHmm’),’ ‘,”)) >= 1

ddwrt:FormatDateTime(string(@Expires),1033,’yyyyMMdd HHmm’) get the Date in ‘yyyyMMdd HHmm’ format i.e 20120612 0230

translate method removes the space between 20120612 0230

Then string is converted to number and the values are subtracted to get results

Click Ok.

Preview the page. Items will be filtered by Date as well as Time.

Announcement WebPart

Announcement WebPart using SharePoint Designer 2010

1. Create Announcement list

The list will have 3 main columns Title (Single line of text), Body (Multi lines of text) and Expires (Date & Time)

2. Create page to render active announcements

Open the page in split mode

Image

Empty data view web part will render

In design mode, click on the link “click here to select data source”

Choose Announcement list as Data Source from data source picker.

On Right Hand Data Source Details panel will open.

Choose the columns, Title, Body and Expires and drag drop on the page

Image

Click on the webpart in Design mode, Data View Tools panel is visible in Top Ribbon

Image

Click on Filter to filter announcement by expiry date

Click Ok

Click on Sort & Group from Top Ribbon, to sort according to Expiry date

Click Ok.

And it will render the Active Announcements