1

I am trying to create remote event receiver on the list which is created in my app. I want to create SharePoint group when I add item in the list.
I have questions like:

  1. Is it possible to create group in AppWeb? Though the event receiver of my list will create ClientContext of appweb URL.
  2. If above is not possible then how can I get the HostWeb URL from the ClientContext which is created for the appweb?
  3. Is there any other way to create ClientContext for the hostweb URL from the Remote event receiver created for the list in my app web?

Thank you in advance...!

1

Groups are always created at the root level. An appweb is a subsite, so you cannot create groups inside it. However, you can create groups in Hostweb root site by creating a client context object of HostWeb in case you use APP Event Receiver.

using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))

You need to pass the second parameter as false in TokenHelper.reateAppEventClientContext. You can get host web url in Remote event receiver using query string. hostUrl = new Uri(Request.QueryString["SPHostUrl"]);

Finally you need to give the app Full Control permissions on the Host web. Follwing code can be used to create groups:

private void CreateGroup(GroupCollection collGroup, string groupName, Web oWebsite, ClientContext clientContext)
{
    Group grp = collGroup.Where(g => g.Title == groupName).FirstOrDefault();
    if (grp == null)
    {
        GroupCreationInformation groupCreationInfo = new GroupCreationInformation();
        groupCreationInfo.Title = groupName;
        groupCreationInfo.Description = "Description of " + groupName;
        Group oGroup = oWebsite.SiteGroups.Add(groupCreationInfo);
        RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);
        RoleDefinition oRoleDefinition = oWebsite.RoleDefinitions.GetByType(RoleType.Contributor);
        collRoleDefinitionBinding.Add(oRoleDefinition);
        oWebsite.RoleAssignments.Add(oGroup, collRoleDefinitionBinding);
        clientContext.Load(oGroup, group => group.Title);
        clientContext.Load(oRoleDefinition, role => role.Name);
        clientContext.ExecuteQuery();
    }
}

For more information please see my blog. Here I create groups in APP Event Receiver http://sharepointnadeem.blogspot.com/2014/02/sharepoint-2013-app-event-receivers.html

  • Yes but when I have added my list in my app. So it will create Event receiver for app web. And in that it will not give the option for useAppWeb = true or false. – Rahul Gokani Mar 28 '14 at 11:04
  • That is why I also mentioned how to get Host web url hostUrl = new Uri(Request.QueryString["SPHostUrl"]); – Nadeem Yousuf Mar 28 '14 at 11:29
  • Yes you have mentioned this code but how can I get Request from the cs page of the Event Receiver? – Rahul Gokani Mar 29 '14 at 4:19
0

As shown here: Remote Event Receiver on host web

You can use:

            // Workaround for getting client context. If not used, users are not able to create sites.
            string webUrl = properties.ItemEventProperties.WebUrl;

            Uri webUri = new Uri(webUrl);

            string realm = TokenHelper.GetRealmFromTargetUrl(webUri);

            string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, webUri.Authority, realm).AccessToken;
            using (var clientContext = TokenHelper.GetClientContextWithAccessToken(webUrl, accessToken))
            {

            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.