If you are like me, find yourself marking old code bits as "Obsolete" but have to keep them in your project for backward compatibility - you must be tired of all the visual studio warnings regarding using obsolete code - especially if it is your own code.
In most cases I have an "Upgrade" method that identifies old version installations and upgrade them to newer version constructs, but in this upgrade code I have to use some of the old obsolete code generating these annoying warning.
BUT - I do not wish to disable all Obsolete warnings - some are very important and I do wish VS to keep warning me about them.
So, the proper way to go is to mark specific code bits not to throw any warnings about using obsolete code. Like: telling the VS that within a specific code block I am aware that I am using some obsolete code and I don't want it to show in my build results.
I came accross the simple solution here that saved me a lot of time figuring out the correct pragma statement for this:
http://stackoverflow.com/questions/344630/ignore-obsoleteattribute-compiler-error
Here, Nick Bolton simple says:
"
What about using #pragma to disable the warning around the specfic code?
#pragma warning disable 0612
// Call obsolete type/enum member here
#pragma warning restore 0612
"
And guess what? It did the trick for me!
A blog by the KWizCom SharePoint experts! The KWizCom team, led by its VP R&D Shai Petel, utilizes its experience and knowledge to give a unique insight on Microsoft's SharePoint platform - the leading business collaboration platform.
Tuesday, September 8, 2009
Tuesday, September 1, 2009
KWizCom is going to SharePoint Saturday New York City
Labels:
SharePoint Saturday
Wednesday, August 12, 2009
How do I tell what the next list item ID is?
By Roi Kolbinger - SharePoint Consultant
KWizCom Professional Services – http://www.kwizcom.com/
If you have ever worked with Event Handlers on an item you probably came across the same problem that was bothering me…
I created a ListItem and used the event handler ItemAdding (for a synchronic event). I tried to find out what my ID was but the value I got was zero.
This problem will not occur on ItemAdded (of a non-synchronic event) because the item is created separately and it has its own ID.
If you use the code below on ItemAdding,you will get a zero value on listItem ID.
SPList list = web.Lists[properties.ListId];
SPListItem listItem = list.Items[properties.ListItem.UniqueId];
int itemId = listItem.ID; // itemId always zero on ItemAdding event
To discover the ID (and avoid getting the zero value) of my synchronic event I wrote this code:
int itemId = list.Items[list.ItemCount - 1].ID + 1;
This code will work well as long as you do not delete the last item and do not add more than one item simultaneously. If you do the sum you will get will be incorrect.
I searched for a SharePoint API that would give me the next item ID but found none. It seemed no one had tackled this problem….
After extensive searching I found a solution, brilliant in its simplicity. This is the code:
///
/// Get the next available item id from a list
///
/// < name="site">site
/// < name="listId">listId
/// <>
public static int NextItemID(SPSite site, Guid listId(
{
int id = -1;
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
{
if (site.WebApplication.ContentDatabases.Count > 0(
{
string DBConnString = site.WebApplication.ContentDatabases[0].DatabaseConnectionString;
SqlConnection con = new SqlConnection(DBConnString);
try
{
con.Open();
SqlCommand com = con.CreateCommand();
com.CommandText = String.Format("select tp_NextAvailableId from AllLists where tp_ID = '{0}'", listId.ToString());
id = (int)com.ExecuteScalar();
}
finally
{
con.Close();
}
}
});
return id;
}
...
int itemId = NextItemID(new SPSite(properties.SiteId), properties.ListId);
As you can see, the solution is to connect to the MOSS database and read the ID AllList table. All the information was right there, we simply had to access it.
Now you know how to get the next ID of the ListItem and (hopefully!) Event Handlers will no longer give you any trouble!...
Credits:
The solution detailed in this article was found at: http://suguk.org/forums/permalink/6226/13513/ShowThread.aspx
KWizCom Professional Services – http://www.kwizcom.com/
If you have ever worked with Event Handlers on an item you probably came across the same problem that was bothering me…
I created a ListItem and used the event handler ItemAdding (for a synchronic event). I tried to find out what my ID was but the value I got was zero.
This problem will not occur on ItemAdded (of a non-synchronic event) because the item is created separately and it has its own ID.
If you use the code below on ItemAdding,you will get a zero value on listItem ID.
SPList list = web.Lists[properties.ListId];
SPListItem listItem = list.Items[properties.ListItem.UniqueId];
int itemId = listItem.ID; // itemId always zero on ItemAdding event
To discover the ID (and avoid getting the zero value) of my synchronic event I wrote this code:
int itemId = list.Items[list.ItemCount - 1].ID + 1;
This code will work well as long as you do not delete the last item and do not add more than one item simultaneously. If you do the sum you will get will be incorrect.
I searched for a SharePoint API that would give me the next item ID but found none. It seemed no one had tackled this problem….
After extensive searching I found a solution, brilliant in its simplicity. This is the code:
///
/// Get the next available item id from a list
///
/// < name="site">site
/// < name="listId">listId
/// <>
public static int NextItemID(SPSite site, Guid listId(
{
int id = -1;
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
{
if (site.WebApplication.ContentDatabases.Count > 0(
{
string DBConnString = site.WebApplication.ContentDatabases[0].DatabaseConnectionString;
SqlConnection con = new SqlConnection(DBConnString);
try
{
con.Open();
SqlCommand com = con.CreateCommand();
com.CommandText = String.Format("select tp_NextAvailableId from AllLists where tp_ID = '{0}'", listId.ToString());
id = (int)com.ExecuteScalar();
}
finally
{
con.Close();
}
}
});
return id;
}
...
int itemId = NextItemID(new SPSite(properties.SiteId), properties.ListId);
As you can see, the solution is to connect to the MOSS database and read the ID AllList table. All the information was right there, we simply had to access it.
Now you know how to get the next ID of the ListItem and (hopefully!) Event Handlers will no longer give you any trouble!...
Credits:
The solution detailed in this article was found at: http://suguk.org/forums/permalink/6226/13513/ShowThread.aspx
Subscribe to:
Posts (Atom)
