How do I tell if a sheet belongs to a certain group?
For example, we have a group called RPR and when we create sheets we share them with other users in our organization applying the group to the sheet.
// Get all sheets modified within last day
SmartSheetList = smartsheet
.SheetResources
.ListSheets(new SheetInclusion[] { SheetInclusion.OWNER_INFO }, new PaginationParameters(true, null, null))
.Data
.Where(d => d.ModifiedAt.Value.Date >= DateTime.Now.AddDays(-1).Date)
.ToList();
// Get organization sheets
var orgUserSheets = smartsheet.UserResources.SheetResources.ListSheets(true).Data.ToList();
// get specific group
var group = smartsheet.GroupResources.ListGroups(null).Data.Where(grp => grp.Id == some-id-here).First();
With the code above I can see if a sheet belongs to an organization but I can't tell if that sheet belongs to a certain group. Any help would be appreciated.
You need one more API Request for each sheet to retrieve the list of shares on the sheet. Keep in mind, sheets can be shared directly on the sheet or via a workspace.
To get sheets that are shared with a specific group directly or via a workspace, you can use code something like the below (relying on LINQ for this sample).
SmartsheetClient cl = new SmartsheetBuilder()
.SetAccessToken(ACCESS_TOKEN)
.Build();
var includeAll = new PaginationParameters(true, null, null);
Console.WriteLine("Looking for group " + SOUGHT_GROUP_NAME);
var groups = cl.GroupResources.ListGroups(includeAll);
var soughtGroup = groups.Data.Single((group) => group.Name == SOUGHT_GROUP_NAME);
Console.WriteLine("Found group ID {0} for group {1}.", soughtGroup != null ? soughtGroup.Id.ToString() : "NULL", SOUGHT_GROUP_NAME);
if (soughtGroup == null)
throw new ArgumentException("Group not found");
var sheets = cl.SheetResources.ListSheets(null, includeAll);
Console.WriteLine("Querying through {0} sheets...", sheets.Data.Count);
var sheetsSharedWithGroup = from sheet in sheets.Data
from share in cl.SheetResources.ShareResources.ListShares(sheet.Id.Value, includeAll, ShareScope.Workspace).Data
where share.GroupId == soughtGroup.Id
select new { Sheet = sheet, Share = share };
var found = sheetsSharedWithGroup.ToList();
Console.WriteLine("Found {0} sheets shared with group {1}.", found.Count, SOUGHT_GROUP_NAME);
found.ForEach(foundSheet => Console.WriteLine("Sheet {0} shared with {1} ({2})", foundSheet.Sheet.Name, foundSheet.Share.Name, foundSheet.Share.Type));
NOTE: I submitted a pull request to add support for the specific ListShares overload that returns workspace shares too. This is available in the REST API but wasn't yet in the C# SDK.
NOTE: The above code does not take into account Smartsheet Sights yet. It should be possible using the corresponding REST API for Sights (i.e. List Sights, List Sight Shares, but I don't think that's in the C# SDK yet.