Agregar y Listar Ítems de Lista con el Modelo de Objetos de SharePoint 2010

Una vez creada la lista se puede agregar ítems a la lista usando el Modelo de Objetos Cliente de SharePoint 2010  con el siguiente código.

 

   1:  try
   2:              {
   3:                  SPSite myTopSite = SPContext.Current.Site;
   4:                  {
   5:                      SPWeb myTopWeb = SPContext.Current.Site.RootWeb;
   6:                      {
   7:                          SPList listMyCustomList = myTopWeb.Lists.TryGetList("Milista");
   8:   
   9:  // TryGetList() es un metodo introducido en  SharePoint 2010.
  10:   
  11:                          if (listMyCustomList != null)
  12:                          {                        
  13:                              //Adicionar item en la lista
  14:                              myTopWeb.AllowUnsafeUpdates = true;
  15:                              SPListItem newItem = listMyCustomList.Items.Add();
  16:                              newItem["Title"] = "Titulo";
  17:                              newItem["Description"] = "Descripcion";
  18:                              newItem.Update();
  19:                              myTopWeb.AllowUnsafeUpdates = false;                        
  20:                          }
  21:                      }
  22:                  }
  23:              }
  24:              catch(Exception ex)
  25:              {
  26:               
  27:              }   




Ahora para poder listar los datos de la lista filtrado por un valor usa el siguiente código.


   1:  SPWeb myTopWeb = SPContext.Current.Site.RootWeb;
   2:                  {
   3:                      SPList listMenu = myTopWeb.Lists["MyCustomList"];
   4:   
   5:                      SPQuery objquery = new SPQuery();
   6:   
   7:                      objquery.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'> AquiVatuValorafiltrar</Value></Eq></Where>";
   8:   
   9:                      SPListItemCollection items = listMenu.GetItems(objquery);
  10:   
  11:                      foreach (SPListItem item in items)
  12:                          if (item != null)
  13:                          {                          
  14:                              if (item["Title"] != null)
  15:                              {
  16:                                  if (!string.IsNullOrEmpty(item["Title"].ToString()))
  17:                                  {
  18:                                      string myTitle = item["Title"].ToString();
  19:                                  }
  20:                              }
  21:                          }
  22:                  }



En el objquery, coloque el valor “AquiVatuValorafiltrar” ahí debes colocar el valor por el cual quieres filtrar los ítems de la lista, es decir cuando el campo titulo sea igual a “AquiVatuValorafiltrar”, mostrará un listado con la coincidencia.

Comentarios

Entradas más populares de este blog

Que es un Webpart o Elemento Web en SharePoint?

The type or namespace name ‘SharePoint’ does not exist in the namespace ‘Microsoft’ (are you missing an assembly reference?) – SharePoint 2013 Client Object Model dlls

Extender el periodo de Evalución hasta 240 días en SQL Server y SharePoint Server