SharePoint Online: Show All Tasks Assigned to My Group
Requirement: In SharePoint Online Task List, Get All Tasks Assigned to the Logged-in User and User’s Groups.
If you want to filter task list items assigned to the current user, you can simply set the [Me] filter in view settings. But If a task item is assigned to a group and the logged-in user is a member of that group, how do we show them? Well, there is no way to set such a filter from the web user interface. So, let’s create a new list view using PowerShell to filter task list items assigned to the current user and current user’s groups in SharePoint Online.
Get All Tasks Assigned to the Current User and Current User’s Groups
Although you can edit the query with SharePoint designer, let’s create a new list view using PowerShell, which filters all tasks assigned to the current user and the current user’s groups.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$ListName ="Project Tasks"
$ViewName="My Group Tasks"
Try {
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Check if the View exists in list already
$ViewColl=$List.Views
$Ctx.Load($ViewColl)
$Ctx.ExecuteQuery()
$NewView = $ViewColl | where { ($_.Title -eq $ViewName) }
if($NewView -ne $NULL)
{
Write-host "View '$ViewName' already exists in the List!" -f Yellow
}
else
{
#Define the CAML Query - Filter Assigned to: Current User or Current User's Group
$ViewQuery = "@
<Where>
<Or>
<Eq>
<FieldRef Name='AssignedTo' />
<Value Type='Integer'>
<UserID />
</Value>
</Eq>
<Membership Type='CurrentUserGroups'>
<FieldRef Name='AssignedTo' />
</Membership>
</Or>
</Where> "
$ViewCreationInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
$ViewCreationInfo.Title = $ViewName
$ViewCreationInfo.Query = $ViewQuery
$ViewCreationInfo.RowLimit = "100"
$ViewCreationInfo.ViewFields = @("Title", "Status", "DueDate", "AssignedTo")
#Add List View
$NewView =$List.Views.Add($ViewCreationInfo)
$Ctx.ExecuteQuery()
Write-host "New View Created in the List Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Adding View to List!" $_.Exception.Message
}
and the list view shows tasks assigned to my group and me
This view lets you see all the tasks assigned to your group in one place.
I used CSOM with console application for the same as powershell and SPD not allowed in my organization.
Below is the code I did using reference of above powershell. thanks a lot!!
using System;
using Microsoft.SharePoint.Client;
namespace MyConsoleApplication
{
class Program
{
static void Main(string[] args)
{
try
{
ClientContext clientContext = new ClientContext(“Site URL”);
Web web = clientContext.Web;
List list = web.Lists.GetByTitle(“Workflow Tasks”);
ViewCollection viewColl = list.Views;
// Specify the columns that should be displayed
string[] viewFields = { “Title”, “AssignedTo”, “Status”, “Created” };
string ViewQuery = “@” +
“” +
“” +
“” +
“” +
“” +
“” +
“” +
“” +
” ” +
” ” +
“” +
” “;
// Specifies the properties used to create a new list view
ViewCreationInformation creationInfo = new ViewCreationInformation();
creationInfo.Query = ViewQuery;
creationInfo.Title = “AllMyTasks”;
creationInfo.RowLimit = 100;
creationInfo.ViewFields = viewFields;
creationInfo.ViewTypeKind = ViewType.None;
creationInfo.SetAsDefaultView = false;
viewColl.Add(creationInfo);
clientContext.ExecuteQuery();
Console.WriteLine(“View created success! Press any key to close!”);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
Console.ReadLine();
}
}
}
}
Can we use CSOM to achieve the same? Powershell or spd is not allowed to use in my org.
This is very useful post to me and my team. I was not about this view approach. Saved lot of coding effort and time for me and my team.
Thanks – Jigar