0
votes

I creating a provider hosted app that should create a calendarlist in SharePoint. I want it to be a group calendar later on but first I just need to do step 1: Create a calendar.

I cant find any provider hosted guides on how to create a SharePoint-calendar programmatically. I cant find a ListTemplateStyle for calendar so I tried the event-template, I dont know if its goint to work since there's an error showing up when I try to run this code below:

 Uri hostWeb = new Uri(Request.QueryString["SPHostUrl"]);

        using (var clientContext = TokenHelper.GetS2SClientContextWithWindowsIdentity(hostWeb, Request.LogonUserIdentity))
        {
            Web web = clientContext.Web;
            ListCreationInformation listCreator = new ListCreationInformation();
            listCreator.Title = "CompanyCalendar";
            listCreator.Description = "Workcalendar";
            listCreator.TemplateType = (int)ListTemplateType.Events; //106 = events
            //clientContext.Load(web);
            web.Lists.Add(listCreator);
            clientContext.ExecuteQuery();

        }

System.Net.WebException: The remote server returned an error: (401) Unauthorized.

Line 27:                 //clientContext.Load(web);
Line 28:                 web.Lists.Add(listCreator);
Line 29:                 clientContext.ExecuteQuery();
Line 30: 
Line 31:             }

It seems like im unauthorized to create a list here for some reason. I have web permission set to full control and my cert looks like this:

<appSettings>
<add key="ClientId" value="ebcb24ca-afbb-4822-8887-f91504f3d25f" />
<add key="ClientSigningCertificatePath" value="C:\Certs\HighTrustSampleCert.pfx" />
<add key="ClientSigningCertificatePassword" value="1234" />
<add key="IssuerId" value="11111111-1111-1111-1111-111111111111" />
</appSettings>

Any idéas?

my second question is: What do I have to learn to make the calendar a group calendar? Any hints?

Thanks

2

2 Answers

0
votes

I solved it on my own.

I apperently used a SharePoint developer-site to debugg on (in my case "https://[email protected]"). Since I work on a virtual machine I should use that local server-site instead (in my case "http://SERVER1").

When I login to my virtual machine I login with my windows username and password (in my case the username was "Developer") and to be able to use that login with the codesnippet "Request.LogonUserIdentity" my debug site had to be the virtual server (in my case "http://SERVER1").

After I changed to SERVER1 it worked and ListTemplateType.Events wich is the template for a calendar gets created and the problem is solved.

Question 1: How to create a sharepointcalendar.
Answear: (int)ListTemplateType.Events;

Question 2: System.Net.WebException: The remote server returned an error: (401) Unauthorized.
Answear: Check your debugsite. Make sure that "Request.LogonUserIdentity" or(the user you logged in to windows with) has promission to your site that you debug towards

Question 3: How to set the "group calendar option" to "true" programmatically on a privider hosted app.
Answear: Not solved :(