u/ChanticrowTwoPointOh

I had need to read and set the SharePoint folder color via the API. It is not really documented, that I could find, and I got a lot of conflicting information on how to do it. Now that I've figured it out I wanted to document it somewhere in case others can benefit.

Props to this article that got me pointed in a better direction: https://medium.com/@alectecson/unlocking-the-hidden-color-how-i-retrieved-the-colorhex-of-a-sharepoint-folder-f8440cce6c06

The code below is in C#, but the concepts should apply anywhere you can make REST calls. You should also understand some SharePoint REST API concepts to apply the correct site URL and folder path.

SharePoint folders offer sixteen different colors. It is referenced as the "vti_colorhex" value, but it is not a hex based color value. It is just a string type number value.

Yellow = "0"
Dark_Red = "1"
Dark_Orange = "2"
Dark_Green = "3"
Dark_Teal = "4"
Dark_Blue = "5"
Dark_Purple = "6"
Dark_Pink = "7"
Grey = "8"
Light_Red = "9"
Light_Orange = "10"
Light_Green = "11"
Light_Teal = "12"
Light_Blue = "13"
Light_Purple = "14"
Light_Pink = "15"

When looking up the properties I'm converting the returned JSON into a class.

public class SharePointFolderProperties
{
    public Properties Properties { get; set; }
    public bool Exists { get; set; }
    public bool ExistsAllowThrowForPolicyFailures { get; set; }
    public bool ExistsWithException { get; set; }
    public bool IsWOPIEnabled { get; set; }
    public int ItemCount { get; set; }
    public string Name { get; set; }
    public object ProgID { get; set; }
    public string ServerRelativeUrl { get; set; }
    public DateTime TimeCreated { get; set; }
    public DateTime TimeLastModified { get; set; }
    public string UniqueId { get; set; }
    public string WelcomePage { get; set; }
}
 
public class Properties
{
    public string vti_x005f_colorhex { get; set; }
}

Finally, here are the functions. Apologies for the crappy formatting on Reddit.

The "newColor" value is the number, "1", "6", etc from the color list above.

public async Task<string> GetFolderColorAsync(string siteUrl, string folderPath, string accessToken)
{
    // Note: vti_colorhex is encoded as vti_x005f_colorhex in the URL
    var endpoint = $"{siteUrl}/_api/Web/GetFolderByServerRelativePath(" +
                   $"DecodedUrl='{folderPath}')" +
                   $"?$expand=Properties&$select=*,Properties/vti_x005f_colorhex";
 
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue("Bearer", accessToken);
    httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=nometadata");
 
    var response = await httpClient.GetAsync(endpoint);
    var json = await response.Content.ReadAsStringAsync();
    var result = JsonConvert.DeserializeObject<SharePointFolderProperties>(json);
 
    return result?.Properties?.vti_x005f_colorhex;
}
 
public async Task<bool> SetFolderColorAsync(string siteUrl, string folderPath, string accessToken, string newColor)
{
    var endpoint = $"{siteUrl}/_api/foldercoloring/stampcolor(DecodedUrl='{folderPath}')";
 
    var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue("Bearer", accessToken);
    httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=nometadata");
 
    var colorChangeJson = JsonConvert.SerializeObject(new { coloringInformation = new { ColorHex = newColor } });
    var content = new StringContent(colorChangeJson, Encoding.UTF8, "application/json");
 
    var response = await httpClient.PostAsync(endpoint, content);
    return response.IsSuccessStatusCode;
    // Do your own handling of the response success/failure.
}

Maybe this will save someone, somewhere, some headache.

Edit: Spelling

reddit.com
u/ChanticrowTwoPointOh — 17 days ago

Yes, I know it is a bad idea. Yes, I know SharePoint is not a file server. That said, the powers that be wanted a complex file structure with different permissions at several different levels of the structure. I am managing the whole thing programmatically so setup and maintenance will generally be fine.

My problem is allowing users to navigate through the folder hierarchy to the folders they have permissions on. My understanding from a coworker that's done this before was that limited access on the upper level folders would still allow a user to see those folders even if they could not do anything with them. A user that can only access a folder three levels down should be able to go to the root of the document hierarchy and navigate to his folder.

This does not work though. Users with limited access cannot see the folder structure at all unless they receive a link directly to the folder they can access. I don't know if I was just given incorrect information about how limited access should work, or if I have set up something incorrectly.

Should users will limited access be able to traverse the whole folder structure?

reddit.com
u/ChanticrowTwoPointOh — 23 days ago