简体   繁体   中英

How to Create Group description[About Field or aboutMe property] in AEM programatically?

I wrote a java program to create a Group inside AEM. It is working fine. But along with Group I also need to create another Group aboutMe property.

My code:

ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
session = resourceResolver.adaptTo(Session.class);
UserManager userManager = ((JackrabbitSession) session).getUserManager();
JackrabbitSession js = (JackrabbitSession) session;
Group group = null;
group = userManager.createGroup("TestGroup");
session.save();

Is there any way to add aboutMe property field also?

You could do group.setProperty("jcr:description","your string") or for that matter any property. Refer to javadocs here

Update

Add dependency to your project pom for com.adobe.granite.security.user

Inject

  @Reference
  private UserPropertiesService service;

Get UserProperties Object (resource is the resource instance to your Group) -

if (this.service != null) {
        Authorizable authorizable = (Authorizable)resource.adaptTo(Authorizable.class);
        UserProperties userProperties;
        if (authorizable == null)
        {
          UserProperties userProperties = (UserProperties)resource.adaptTo(UserProperties.class);
          if (userProperties != null) {
            UserManager uMgr = (UserManager)resolver.adaptTo(UserManager.class);
            authorizable = uMgr.getAuthorizable(userProperties.getAuthorizableID());
          }

        }
        else
        {
          Session session = ((Node)resource.adaptTo(Node.class)).getSession();
          UserPropertiesManager mgr = this.service.createUserPropertiesManager(session, resolver);
          String propPath = request.getParameter("path");
          userProperties = mgr.getUserProperties(authorizable, propPath);
        }

Once you get the UserProperties, you can add the aboutMe information to your group.

For further reference, read this and the javadocs here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM