public void GenerateInwardNumber(SPItemEventProperties properties)
{
try
{
int autoid = 101;
SPWeb oWeb = properties.Web;
oWeb.AllowUnsafeUpdates = true;
SPList oList = properties.List;
SPListItem oItem = properties.ListItem;
string[] s = Convert.ToString(oItem["Location"]).Split('#');
string location = s[s.Length - 1];
SPQuery qry = new SPQuery();
qry.Query = "<Where><Eq><FieldRef Name='Location1' /><Value Type='Lookup'>" + location + "</Value></Eq></Where><OrderBy><FieldRef Name='Document_x0020_Inward_x0020_Number' Ascending='False' /></OrderBy>";
SPListItemCollection oItemColl = oList.GetItems(qry);
string locCode = GetLocationCode(location, properties.Web);
string oldDIN = Convert.ToString(oItemColl[0]["Document_x0020_Inward_x0020_Number"]);
string[] str = oldDIN.Split('-');
if (!string.IsNullOrEmpty(oldDIN))
autoid = Convert.ToInt16(str[str.Length - 1]) + 1;
else
autoid = 101;
SPListItem oCurrItem = properties.ListItem;
oCurrItem["Document_x0020_Inward_x0020_Number"] = locCode + "-" + autoid;
this.EventFiringEnabled = false;
oCurrItem.SystemUpdate(false);
this.EventFiringEnabled = true;
oWeb.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
properties.ErrorMessage = ex.Message;
}
}
public string GetLocationCode(string location,SPWeb cWeb)
{
string locCode="";
try
{
SPQuery locqry = new SPQuery();
locqry.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + location + "</Value></Eq></Where>";
SPList locList = cWeb.Lists["Locations"];
SPListItemCollection oItemColl = locList.GetItems(locqry);
locCode = Convert.ToString(oItemColl[0]["Location_x0020_Code"]);
}
catch (Exception ex)
{
}
return locCode;
}