1

I'm trying to work with high trust apps on SharePoint 2013.

I'm using Visual Studio 2013 Update 3, SharePoint 2013 Enterprise SP1.

I've followed this 2 links :

Create a basic provider hosted app

http://msdn.microsoft.com/en-us/library/office/fp142381(v=office.15).aspx

Package and publish high-trust apps for SharePoint 2013

http://msdn.microsoft.com/en-us/library/office/jj860570(v=office.15).aspx

I've set up correctly everything for authentication etc...

I use the default provider hosted app, MVC web app.

When I hit F5, IIS Express, lauch and everything is working.

But when I'm adding, a custom list with some items in my App Project, and try to access from my HomeController, I have this 401 Unauthorized error, at the clientContext.ExecuteQuery() instruction.

//This is the default code, working it access the hostweb and displays my user name account

        User spUser = null;

        var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

        using (var clientContext = spContext.CreateUserClientContextForSPHost())
        {
            if (clientContext != null)
            {
                spUser = clientContext.Web.CurrentUser;

                clientContext.Load(spUser, user => user.Title);

                clientContext.ExecuteQuery();

                ViewBag.UserName = spUser.Title;
            }
        }

But when I'm trying to access the appWeb to read my list items, it's not working.

Here's my code :

        CamlQuery queryProducts = new CamlQuery();
        queryProducts.ViewXml = @"<View><ViewFields><FieldRef Name='Title'/>
                            <FieldRef Name='ProductDescription'/>
                            <FieldRef Name='Price'/></ViewFields></View>";

        List<Product> products = new List<Product>();

        using (var clientContext = spContext.CreateUserClientContextForSPAppWeb())
        {
            if (clientContext != null)
            {
                //TEST to verify logged user

                var tmp = Request.LogonUserIdentity;

                List lstProducts = clientContext.Web.Lists.GetByTitle("Products");

                ListItemCollection lstProductItems = lstProducts.GetItems(camlQuery);

                clientContext.Load(lstProductItems);

                clientContext.ExecuteQuery();

                if (lstProductItems != null)
                {
                    foreach (var lstProductItem in lstProductItems)
                    {
                        products.Add(
                            new Product
                            {
                                Title = lstProductItem["Title"].ToString(),
                                Description = lstProductItem["ProductDescription"].ToString(),
                                Price = lstProductItem["Price"].ToString()
                            });
                    }
                }
            }
        }

The problem is that, I get this error in debugging with IIS Express, or when I publish my app to a remote server.

The account I'm logged with, is site collection admin.

Does anybody, have an idea ?

Thanks in advance for answers.

Abdel

  • Check this Question: (If this can help) sharepoint.stackexchange.com/questions/70318/… nerate-clientid-and-secret-for-app – Rahul Gokani Nov 14 '14 at 10:21
  • Thanks for this, but I didn't get any problems registering the app. Also I have followed the instructions of Todd Klindt for dns zone for apps, also setup in central admin. The problem is weird, because when I remove the code accessing the list of the app web, everything is working fine... – Abdel Nov 14 '14 at 10:45

Your Answer

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

Browse other questions tagged or ask your own question.