Sunday, December 27, 2009

HttpContext & SPContext is always null when Uploading Multiple document libraryin sharepoint

HttpContext & SPContext is always null when Uploading Multiple document libraryin sharepoint : I'm tring to get a value from the session and update a custom field when uploading multiple documents to my sharepoint document library.

I faced an issue when I was tring to update some custom fields of my document library when uploading multiple documents, the field was ( ProjectID ) which I put it inside a session in my custom webpart (the step before uploading the documents).

What I did is : I put the projectID into the cache ( per user which acts as a session) inside the custom webpart as follows :

if (Request.QueryString["ProjectID"] != null)

{

HttpRuntime.Cache.Remove(SPContext.Current.Web.CurrentUser.LoginName);

HttpRuntime.Cache.Add(SPContext.Current.Web.CurrentUser.LoginName, ProjectID, null, DateTime.UtcNow.AddMinutes(60), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);

}

then I implemented the ItemAdded event and I get the value of the cached projectId through :

Note that I get the cached key through the created by field which is the same key name that I cached it from inside my custom webpart.

public override void ItemAdded(SPItemEventProperties properties)

{

try

{

string ProjID = "";

string CreatedBy = null;

if (properties.ListItem["Created By"] != null)

CreatedBy = properties.ListItem["Created By"].ToString().Split(';')[1].Replace("#","");

if (HttpRuntime.Cache[CreatedBy] != null)

{

//SPContext.Current.Web.CurrentUser.LoginName;

ProjID = HttpRuntime.Cache[CreatedBy].ToString();

if (properties.ListItem["Project"] == null)

{

properties.ListItem["Project"] = new SPFieldLookupValue(ProjID);

properties.ListItem.SystemUpdate();

}

base.ItemAdded(properties);

}

}

catch (Exception ex)

{ }

}

Regards,

Mohammed Barakat Kharboush

No comments: