sábado, 25 de febrero de 2012

Obtener el próximo ID de una lista de Sharepoint 2010

El campo ID es un campo autoincremental.

   1:  public static int NextItemID(SPSite site, Guid listId)
   2:  {
   3:  int id = -1;
   4:  SPSecurity.RunWithElevatedPrivileges(delegate()
   5:  {
   6:  if (site.WebApplication.ContentDatabases.Count > 0)
   7:  {
   8:  string DBConnString = site.WebApplication.ContentDatabases[0].DatabaseConnectionString;
   9:  SqlConnection con = new SqlConnection(DBConnString);
  10:  try
  11:  {
  12:  con.Open();
  13:  SqlCommand com = con.CreateCommand();
  14:  com.CommandText = String.Format(“SELECT NextAvailableId FROM AllListsAux where ListID = ‘{0}’”, listId.ToString());
  15:  id = (int)com.ExecuteScalar();
  16:  }
  17:  finally
  18:  {
  19:  con.Close();
  20:  }
  21:  }
  22:  });
  23:  return id;
  24:  }





Campo: tp_NextAvailableId
Descripción: Next available ID for an item that is added to a list.

En Sharepoint 2007 cambia la consulta SQL:

SELECT tp_NextAvailableId FROM AllLists where tp_ID='{Id of the List}'

Fuente:

http://sprider.org/2011/10/15/get-next-list-item-id-in-sharepoint-list/

http://kwizcom.blogspot.com/2009/08/how-do-i-tell-what-next-list-item-id-is.html

No hay comentarios:

Publicar un comentario