Issues with List Attachments in K2.NET 2003.

  • 2 December 2008
  • 0 replies
  • 0 views

Badge

We have been greatly plagued by the attachment issue with SharePoint lists in our organizaton where once a process is submitted, we lose the handle on that process, because the connection object does not give us a return type. I have looked through various articles and scratched my head on how to go about this scenario. We were using threads(letting the thread sleep for 5 seconds) as our mechanism. This gave enough time for the list item to be generated and then based on the list ID that is created, call the upload functionality in our C# code behind. The downside of this is that if there are any network latency or for some other reason the transaction takes a longer time to execute, the sleep time of the thread is over and it goes to the upload functionality without the list ID being generated. This caused the attachments not to go through as there is no associated List ID.


To overcome this scenario and ofcourse I have to mention Blake's (from Source Code) name for his inputs also, we have removed the upload functionality from our C# code behind in our .NET solution and added the same in K2. So for the activity that has the event for pushing data into SharePoint, we have also added another activity below this called Upload Event. This will always ensure that the SharePoint list gets created before it moves to the next level, because the activity is never complete until the List ID gets generated. Also, please note, we have a few binary fields for attachments as Process Instance variables. The ASP.NET code just pushes the binary data and all relevant Process instance values to K2. Note, we do not send the Binary Data in our first activity when we create the list. In the next event which is the upload event, we push the binary values against the SharePoint list item.


 Here is a code snippet from the upload activity. Please include the Microsoft.SharePoint.dll for this in the K2.NET 2003 studio.


public void Main(ServerEventContext K2)
{
 K2.Synchronous = true;
 
 Microsoft.SharePoint.SPSite siteCollection = null;
 Microsoft.SharePoint.SPWeb topLevelSite = null;
 Microsoft.SharePoint.SPList list = null;
 Microsoft.SharePoint.SPListItemCollection myListItemCol = null;
        
 try
 { 
          
  siteCollection = new Microsoft.SharePoint.SPSite(K2.StringTable["SPSiteCollection"]);
  topLevelSite = siteCollection.AllWebs[K2.StringTable["SPTopLevelSite"]];
  list = topLevelSite.Lists[K2.StringTable["SPListName"]];
  siteCollection.OpenWeb();
  
  siteCollection.AllowUnsafeUpdates = true;
  topLevelSite.AllowUnsafeUpdates = true; 
  
  myListItemCol = list.Items; 
  
  
  if (!string.IsNullOrEmpty(K2.ProcessInstance.DataFields["PR_FileUpload_QuoteName"].Value.ToString().Trim()))
  {
  
   foreach (Microsoft.SharePoint.SPListItem item in myListItemCol)
   {     
    if (K2.ProcessInstance.DataFields["ListItemID"].Value.ToString().Trim().Equals(Convert.ToString(item["ID"].ToString().Trim()).Trim()))
    {   
     if (!string.IsNullOrEmpty(K2.ProcessInstance.DataFields["PR_FileUpload_QuoteName"].Value.ToString().Trim()))
     {
      item.Attachments.Add(K2.ProcessInstance.DataFields["PR_FileUpload_QuoteName"].Value.ToString().Trim(),(byte[]) K2.ProcessInstance.DataFields["PR_FileUpload_Quote"].Value);}


}


}


}


 


0 replies

Be the first to reply!

Reply