Created
July 6, 2021 09:22
-
-
Save ivanbuzyka/9cbe45dfa41bdd3efc6439a3340ea18b to your computer and use it in GitHub Desktop.
Sitecore Content Hub. Changing entity definition programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Stylelabs.M.Sdk.Models.Base; | |
using Stylelabs.M.Sdk.Models.Base.PropertyDefinitions; | |
using Stylelabs.M.Sdk.WebClient; | |
using Stylelabs.M.Sdk.WebClient.Authentication; | |
using System; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace SchemaModification | |
{ | |
class Program | |
{ | |
private static Random random = new Random(); | |
static async Task Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
var client = CreateClient(); | |
var def = await client.EntityDefinitions.GetAsync("CNHI.Line"); | |
var mg = new MemberGroup(); | |
var name = $"Group{RandomString(4)}"; | |
mg.Name = name; | |
var stringProperty = new StringPropertyDefinition() | |
{ | |
Name = "Property1", | |
// possible conditions. | |
//Conditions = new List<IMemberCondition>() { new RelationMemberCondition() { } } | |
//Conditions = new List<IMemberCondition>() { new BoolMemberCondition() { } } | |
//Conditions = new List<IMemberCondition>() { new StringMemberCondition() { } } | |
//Conditions = new List<IMemberCondition>() { new NoAncestorMemberCondition() { } } | |
}; | |
mg.MemberDefinitions.Add(stringProperty); | |
def.MemberGroups.Add(mg); | |
var definition = await client.EntityDefinitions.SaveAsync(def); | |
Console.WriteLine("Done"); | |
Console.ReadLine(); | |
} | |
internal static IWebMClient CreateClient() | |
{ | |
Uri endpoint = new Uri(Settings.ContentHub.ContentHubUrl); | |
OAuthPasswordGrant oauth = new OAuthPasswordGrant | |
{ | |
ClientId = Settings.ContentHub.ClientId, | |
ClientSecret = Settings.ContentHub.ClientSecret, | |
UserName = Settings.ContentHub.Username, | |
Password = Settings.ContentHub.Password | |
}; | |
IWebMClient client = MClientFactory.CreateMClient(endpoint, oauth); | |
return client; | |
} | |
public static string RandomString(int length) | |
{ | |
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
return new string(Enumerable.Repeat(chars, length) | |
.Select(s => s[random.Next(s.Length)]).ToArray()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment