Programmatically create profile?

Discussions related to custom development with Select.
Post Reply
goulet
Posts: 16
Joined: Sun Feb 09, 2014 5:07 pm

Programmatically create profile?

Post by goulet »

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!
goulet
Posts: 16
Joined: Sun Feb 09, 2014 5:07 pm

Re: Programmatically create profile?

Post by goulet »

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:

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;
Step 2: Create tree node items and add them to the root element and/or to the newly created nodes:

Code: Select all

IProfileTreeItem newPTI = profileMgr.NewProfileTreeItem("My Profile");
pti.Children.Add(newPTI);
Step 3: Apply the changes to persist:

Code: Select all

profileMgr.ApplyChanges(new IProfileTree[] { ptree } );
BobRichards
Posts: 1377
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Programmatically create profile?

Post by BobRichards »

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
goulet
Posts: 16
Joined: Sun Feb 09, 2014 5:07 pm

Re: Programmatically create profile?

Post by goulet »

Excellent, thanks! Glad to see I was on the right track.
Post Reply