Is task assigned to user

SP2013

Hello everyone,

Today I’ll talk about a piece of code that I used recently to determine whether a workflow task is assigned to the person who created it.

The following code shows whether a task is assigned to the task creator or to one of his groups, even if it is not added namely in the SharePoint group but via an Active Directory group.

Firstly, we first obtain the value of fields “Created by” and “Assigned To”

SPFieldUser authorUserField = (SPFieldUser)task.Fields.GetField(“Author”);

SPFieldUserValue authorUserFieldValue = (SPFieldUserValue)authorUserField.GetFieldValue(task[“Author”].ToString());

 

SPFieldUser assignedUserField = (SPFieldUser)task.Fields.GetField(“AssignedTo”);

SPFieldUserValue assignedUserFieldValue = (SPFieldUserValue) assignedUserField.GetFieldValue(task[“AssignedTo”].ToString());

 

We test first whether the field “assigned to” contains a user

If (assignedUserFieldValue.User!=null)

{

SPUser assignedUser = assignedUserFieldValue.User;

isAssignedToUser =(assignedUser.ToString() == authorUserFieldValue.User.ToString());

}

 

If this is not the case , we test whether the group contains the user wanted.

To do this, we will use the “SPGroup.ContainsCurrentUser” function which also works if the user is part of an active directory group added to SharePoint group!

To use this function, we will use the usertoken of the desired user.

 

Here’s the code to do this:

isAssignedToCurrentUser=false;

SPGroup assignedGroup = task.Web.SiteGroups.GetByID(assignedUserFieldValue.LookupId) ;

SPSecurity.RunWithElevatedPrivileges( delegate

{

using(SPSite elevatedSite = new SPSite (task.Web.Site.ID))

{

using(SPWeb elevatedWeb = elevatedSite.OpenWeb(task.Web.ID))

{

SPuser currentUser = elevatedWeb.EnsureUser(authorUserFieldValue.User.LoginName);

If (currentUser !=null)

{

using (SPSite currentUserSite = new SPSite(task.web.Site.ID,currentUser.UserToken))

{

using (SPweb currentUserweb = currentUserSite.OpenWeb(task.Web.ID))

{

isAssignedToCurrentUser=assignedGroup.ContainsCurrentUser;

}

}

}

}

} 

});

 

With this code, there is a case that is not managed, when the task is directly assigned to the active directory group.

Hope this helps.

Christopher.

Advertisements

SharePoint 2013 – Send queries to a custom results page URL disabled

SP2013

Hello everyone,

Today I ‘ll talk about a side effect that may occur in SharePoint 2013.

If you try to change « Which search results page should queries be sent to ? » for the site collection, you can uncheck the « Use the same results page settings as my parent » box.

 

However, after unchecking this box, you realize that you can’t fill the « Results page URL » !

 

This is because the URL is generated by the new feature ” Minimal Download Strategy “! If you want to modify this data, several options are available

  • Disable feature Minimal Download Strategy from the ” Manage Site Features ” menu
  • Enter the correct url: “ /_layouts/15/enhancedSearch.aspx ?level=sitecol”

Another option is to navigate the “Search settings” page (in the Search of the sites settings) click cancel (which has the effect of redirecting you to the real page settings url) and click again on “Search settings” of the ” Site Collection Administration” and voila!

 

Hope this helps

 

 Christopher.