--
public static int NextListItemID(SPSite oSite, Guid listId)
{
    int listItemId = -1;

    Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        if (oSite.WebApplication.ContentDatabases.Count > 0)
        {
            //Get the connection string for the sharepoint database
            string connString = oSite.WebApplication.ContentDatabases[0].DatabaseConnectionString;

            //Establish a connection
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connString);

            try
            {
                con.Open();

                //Query to get the next item id for a list(Filtering the list by the list id)
                System.Data.SqlClient.SqlCommand com = con.CreateCommand();
                com.CommandText = String.Format("select tp_NextAvailableId from AllLists where tp_ID = '{0}'", listId.ToString());
                listItemId = (int)com.ExecuteScalar();
            }
            finally
            {
                con.Close();
            }
        }
    });

    return listItemId;
}

posted on 2012-03-12 09:22  blogsweb  阅读(167)  评论(0)    收藏  举报