0

My application creates a calendar at my site, I have created a view in my code that creates a view for each person that is in my custom contactlist.

In my contactlist I have a "User" field which is my username (in my case "Developer").

When I create a view in my app I use CAML to filter all events created by this contacts username/login name. But when I select a view, create a new event and save, the event doesent show up in that view, it only shows up in the default "Calendar" view.

What have a missed out? Shouldn't the CAML filter show this new events created by this contacts username?

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;

            List ifListExcists;
            // ValidateList() is a custo method to validate if the list already excists
            if (!ValidateList(clientContext, listCreator.Title, out ifListExcists))
            {
                List calList = web.Lists.Add(listCreator);
                clientContext.ExecuteQuery();
                testLabel.Text = "The " + listCreator.Title + " list was created";

                List contactList = web.Lists.GetByTitle("Contacts");
                CamlQuery query = CamlQuery.CreateAllItemsQuery();
                Microsoft.SharePoint.Client.ListItemCollection collection = contactList.GetItems(query);
                clientContext.Load(collection);
                clientContext.ExecuteQuery();

                foreach (var thisPerson in collection)
                {
                    List companyCalendarList = web.Lists.GetByTitle("CompanyCalendar");
                    Microsoft.SharePoint.Client.View view = companyCalendarList.Views.GetByTitle("Calendar");
                    clientContext.Load(view);
                    clientContext.ExecuteQuery();

                    //Find the username for this user in cantactlist
                    var loggedInUserName = thisPerson["loggedInUser"];

                    //Get to LastName of (Req field) in the contactlist just for testing
                    string currentUserName = thisPerson["Title"].ToString();

                    //Create a new CalendarView
                    ViewCreationInformation newView = new ViewCreationInformation();
                    newView.Title = currentUserName;
                    newView.Query = "<Where><Eq><FieldRef Name='Author' /><Value Type='User'>" + loggedInUserName + "</Value></Eq></Where>";


                    calList.Views.Add(newView);
                    clientContext.ExecuteQuery();

                }

            }
            else
            {
                testLabel.Text = "List already excist";
            }
        }
Hannes
  • 140
  • 1
  • 3
  • 18

1 Answers1

0

Do this and it will work!

var loggedInUserName = thisPerson["loggedInUser"] as Microsoft.SharePoint.Client.FieldLookupValue;
int userId = loggedInUserName.LookupId;

//Create a new CalendarView
ViewCreationInformation newView = new ViewCreationInformation();
newView.Query = "<Where><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Lookup'>" + userId + "</Value></Eq></Where>";
Hannes
  • 140
  • 1
  • 3
  • 18