This code is from a sequential workflow. I am trying to grab the most recent item in a document library. I check to make sure there are more than zero items in the library, then I try to grab the most recent. However, when I try this, I get an "Object reference not set to an instance of an object." error. I have used this exact same code in other places for different document libraries and it works just fine. I can call the library, get an ItemCount from it, but I can't grab any of the items. Could there be some special permission or lock on this document library that I don't know about?
if (tlList.ItemCount > 0)
{
SPListItem item = tlList.Items[tlList.ItemCount - 1];
name = item["Name"].ToString();
user = item["Modified By"].ToString();
url = SharepointState.siteUrl + "/" + SharepointState.TLLibraryName + "/" + DocumentState.TrackingNumber.Replace(':', '_');
itemNumber = (ulong)DateTime.Now.Ticks;
}
Edit: I just found some code in the Feature Event Receiver that also calls on tlList and does it successfully. Here is the code:
tlList = SharepointState.Web.Lists.TryGetList(SharepointState.TLLibraryName);
SharepointState.Web.AllowUnsafeUpdates = true;
SPList workflowhistory = SharepointState.Web.Lists["Workflow History"];
SPList tasks = SharepointState.Web.Lists["Tasks"];
SPWorkflowAssociation spas = null;
spas = SPWorkflowAssociation.CreateListAssociation(baseTemplate, "NSI_FCS_Workflow", tasks, workflowhistory);
spas.AutoStartCreate = true;
spas.AutoStartChange = true;
tlList.WorkflowAssociations.Add(spas);
tlList.Update();
SharepointState.Web.AllowUnsafeUpdates = false;
Perhaps I need to 'dispose' of the document library before trying to grab it again when the workflow starts? Perhaps I am doing something to the document library in this code that is preventing me from accessing it again in the future? Let me know if you have any suggestions.