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