Is there any way to programmatically create a profile using the APIs? IProfileManager has a 'NewProfileTreeItem', but the resulting IProfileTreeItem is not compatible with either of the 'IProfileManager.ApplyChanges' methods.
Thanks!
Programmatically create profile?
Re: Programmatically create profile?
Found the answer. The trick is to dig into the IProfileManager.ProfileTrees. Here you can add nodes to the 'Children' list.
Step 1: Get the profile manager and the root profile:
Step 2: Create tree node items and add them to the root element and/or to the newly created nodes:
Step 3: Apply the changes to persist:
Step 1: Get the profile manager and the root profile:
Code: Select all
IProfileManager profileMgr = server.GetService<IProfileManager>();
IProfileTree ptree = profileMgr.ProfileTrees.Where(pt => pt.Name == "Default").FirstOrDefault(); // Would it be better just get 'ProfileTrees.First'?
IProfileTreeItem pti = ptree.Root;
Code: Select all
IProfileTreeItem newPTI = profileMgr.NewProfileTreeItem("My Profile");
pti.Children.Add(newPTI);
Code: Select all
profileMgr.ApplyChanges(new IProfileTree[] { ptree } );
-
- Posts: 1377
- Joined: Wed Jan 15, 2014 3:50 pm
- Location: Raleigh, NC
- Contact:
Re: Programmatically create profile?
You just beat me to answering this. Here is an example of adding a profile directly under Default that can be expanded as needed. It also shows how to set properties on the new profile.
Code: Select all
// Get top level tree node.
IProfileTree parentProfileTree = profileMgr.ProfileTrees.First(t => t.Name == "Default");
IProfileTreeItem parentProfileTreeItem = parentProfileTree.Root;
// New profile name.
string name = "Test Profile";
IProfileTreeItem newProfileTreeItem = profileMgr.NewProfileTreeItem(name);
// Add new profile under parent.
parentProfileTreeItem.Children.Add(newProfileTreeItem);
profileMgr.ApplyChanges(parentProfileTree);
// Get profile and set properties.
IProfile newProfile = profileMgr.FindProfile(newProfileTreeItem.Path);
// Can use the properties of this object to get/set LocationName, Members, OrdersCreatedAs, ...
// Example of adding simple properties.
newProfile["LocationName"] = "Raleigh Site";
newProfile["LocationCity"] = "Raleigh";
newProfile["SalesTax"] = true;
// Example of adding a User or Group to the profile membership.
ISecurityIdentity newMember = profileMgr.ActiveProfile.Members.First(t => t.Enabled);
IList<ISecurityIdentity> members = newProfile["Members"] as IList<ISecurityIdentity>;
members.Add(newMember);
// Enable orders created with this profile to retain the association (instead of Default).
newProfile["OrdersCreatedAs"] = newProfile;
// Save all the properties you just set.
profileMgr.ApplyChanges(newProfile);
Bob Richards, Senior Software Developer, SoftPro
Re: Programmatically create profile?
Excellent, thanks! Glad to see I was on the right track.