1

I created an provider hosted app in vs. In the app I have a mvc web project. In the controller I have the SPContext and can do SP logic. I created some helper class. In this helper class I would also do some SP logic, but I dont have the SPContext. What is the best practice to get the SPContext in a seperated helper class?

I try this code below but got an access denied error:

    public class DropdownHelper
        {
            internal static ClientContext spClientContext;
            internal static ClientContext SPClientContext
            {
                get
                {
                    if (spClientContext == null)
                    {
                        spClientContext = GetSPContext();
                    }

                    return spClientContext;
                }

                set {
                    spClientContext = value;
                }
            }

            internal static ClientContext GetSPContext()
            {
                return SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current).CreateAppOnlyClientContextForSPHost();
            }

        internal static UserCollection GetUsersByGroupName()
        {
            Group group = SPClientContext.Web.SiteGroups.GetByName("MyCustomGroupName");
            SPClientContext.Load(group, grp => grp.Title, grp => grp.Users);
            SPClientContext.ExecuteQuery(); // here I got the access denied error

            if (group != null)
            {
                return group.Users;
            }

            return null;
        }
    }

ps. when I run this code in the controller with the SPContext from the controller it is working fine.

2 Answers 2

0

Do not get SP context class in helper class, Pass it as parameter to helper class... you can also create property and assign it to instance of helper class before calling actual methods. below is what i can think of. Though I have not tried below but as per OOPS concept it should work.

public class DropdownHelper
        {


            private ClientContext spClientContext;
            public  ClientContext SPClientContext
            {
                get
                {
                     return spClientContext;
                }

                set {
                    spClientContext = value;
                }
            }



        internal static UserCollection GetUsersByGroupName()
        {
            Group group = SPClientContext.Web.SiteGroups.GetByName("MyCustomGroupName");
            SPClientContext.Load(group, grp => grp.Title, grp => grp.Users);
            SPClientContext.ExecuteQuery(); // here I got the access denied error

            if (group != null)
            {
                return group.Users;
            }

            return null;
        }
    }

Now from calling class use below code.

DropdownHelper helper = new DropdownHelper();
helper.SPClientContext =  SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current).CreateAppOnlyClientContextForSPHost();
UserCollection collecton = helper.GetUsersByGroupName();
0

I found the issue why I got the access denied. I was needed to use "CreateUserClientContextForSPHost" instead of "CreateAppOnlyClientContextForSPHost".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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