Why doesn't my calendar view show added events - something wrong with my CAML?
-
28-09-2020 - |
Question
My code is pretty straight forward. I create a Calendar list and some views for the calendar.
I create one view for each contact in my custom contact list (containing a "User" field which contains that person's login username).
All of this is created when I start my provider hosted app.
The thing is that when I add a calendar event I should be able to see that event in my view. In my custom calendar user field I have added my username "Developer" in my case. (see picture)
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 custom 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";
//Get my custom contactlist that contains a User field that contains the login username of that contact
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)
{
//Find the username for this user in cantactlist
var loggedInUserName = thisPerson["loggedInUser"];
//Get the internal name for LastName field (Which is the only Required field in this list) in the contactlist just for testing
string currentUserName = thisPerson["Title"].ToString();
//Create a new CalendarView
ViewCreationInformation newView = new ViewCreationInformation();
newView.Title = currentUserName;
//Show events that a re "created by"(Author) thisPerson["loggedInUser"]
newView.Query = "<Where><Eq><FieldRef Name='Author' /><Value Type='User'>" + loggedInUserName + "</Value></Eq></Where>";
calList.Views.Add(newView);
clientContext.ExecuteQuery();
}
}
else
{
//I don't think I need this but I post it just to show that its still not working even though I try to update the views.
Microsoft.SharePoint.Client.ViewCollection viewCollection = web.Lists.GetByTitle("CompanyCalendar").Views;
clientContext.Load(viewCollection);
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.View view in viewCollection)
{
view.Update();
clientContext.ExecuteQuery();
}
testLabel.Text = "List already excist";
}
}
This shows after I add an event logged in as "Developer" (which is the username inside "Bergsten"-view).
My Question: Why doesn't this view show the events that I'm adding? They do show in default the calendar view but not this "Bergsten"-view. The CAML filtering doesn't seems to work. Is there something that I'm missing or is it something wrong with my view.Query?
Solution
Can you try like this:
var loggedInUserName = thisPerson["loggedInUser"] as Microsoft.SharePoint.Client.FieldLookupValue;
int userId = loggedInUserName.LookupId;
//Create a new CalendarView
ViewCreationInformation newView = new ViewCreationInformation();
//Show events that are "created by"(Author) thisPerson["loggedInUser"]
newView.Query = "<Where><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Lookup'>" + userId + "</Value></Eq></Where>";
This should work..