¿Por qué mi vista de calendario no muestra eventos añadidos - algo mal con mi caml?
-
28-09-2020 - |
Pregunta
Mi código es bastante sencillo. Creo una lista de calendario y algunas vistas para el calendario.
He creado una vista para cada contacto en mi lista de contactos personalizados (que contiene un campo "Usuario" que contiene el nombre de usuario de inicio de sesión de esa persona).
Todo esto se crea cuando comienzo mi proveedor de aplicaciones alojadas .
La cosa es que cuando agrego un evento de calendario, debería poder ver ese evento en mi opinión. En mi campo de usuario calendario personalizado, he agregado mi nombre de usuario "desarrollador" en mi caso. (ver foto)
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";
}
}
Esto muestra después de agregar un evento que ha iniciado sesión como "Desarrollador" (que es el nombre de usuario dentro "Bergsten" -View).
My Pregunta: ¿Por qué esta opinión no muestra los eventos que estoy agregando? Muestran por defecto la vista del calendario, pero no esta "Bergsten" -View. El filtrado de Caml no parece funcionar. ¿Hay algo que me esté perdiendo o si es algo malo con mi vista.Query?
Solución
¿Puedes intentarlo así:
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>";
Esto debería funcionar ..