2009-10-21 5 views
1

MOSS의 문서 라이브러리에서 이벤트 처리기 내에서 HTTPContext를 가져 오려고하는데 HTTPContext.Current의 null 값만 있습니다. 동일한 작업을 수행합니다. List 및 HTTPContext가 돌려 주어집니다. HTTPContext.Request 메서드에 액세스하기 위해 문서 라이브러리에서 HTTPContext를 가져 오는 방법이 있습니까? 당신의 도움이 여기에이벤트 처리기에서 HttpContext를 구하는 방법

에 대한

덕분에 코드입니다 :

public class TestContextListItemEventReceiver : SPItemEventReceiver 
{ 
    HttpContext current; 
    static object obj; 

    /// <summary> 
    /// Initializes a new instance of the Microsoft.SharePoint.SPItemEventReceiver class. 
    /// </summary> 
    public TestContextListItemEventReceiver() 
    { 
     current = HttpContext.Current; 
    } 

    public override void ItemAdding(SPItemEventProperties properties) 
    { 
     obj = current; 
    } 
} 

답변

2

아이템 이벤트 수신기가 비동기 적으로 실행; 이벤트를 시작한 HTTP 요청에 액세스 할 수 없습니다.

+0

이벤트 수신자의 ItemAdding 메소드와 모든 메소드는 동기식이므로 HTTP 요청의 컨텍스트를 catch 할 수 있습니다. 사실, 이벤트 수신기에서 코드를 테스트하여 HttpContext를 반환하지만 문서 라이브러리를 사용하려고하면 컨텍스트가 null입니다. –

0

SharePoint 인터페이스 (Internet Explorer)에서 문서를 업로드하면 SPList 및 문서 라이브러리에서 HttpContext를 잡을 수 있습니다. 그러나 Microsoft Word에서 문서를 저장하면 HttpContext를 잡을 수 없으며 그 이유를 알 수 없습니다.

0

사용자가 하나의 문서를 업로드하려고하면 내가 ItemAdding 이벤트 내부에서 세션 객체를 얻을 수있는 httpRuntime을 클래스

0

를 사용해보십시오,하지만 문제는 사용자가 사용하여 여러 문서를 업로드 할 때 httpcontext.current는 항상 null입니다 문서 libarary 옵션 (여러 문서 업로드)

4

새 문서를 업로드 할 때 내 문서 라이브러리의 일부 사용자 정의 필드를 업데이트하려고 할 때 같은 문제가 발생했습니다.이 필드는 (세션 ID) 내 webpart (문서를 업로드하기 전에 단계). 나는 다음과 같이 세션 역할을하는 사용자 정의는 WebPart 내부 (사용자 당) 캐시에 projectID을 넣어 : 내가 무슨 짓을

이다

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); 
} 

그런 다음 나는 ItemAdded 이벤트를 구현하고 나는 값을 얻을 의 캐시 projectId을 통해 :

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) 
    { } 
} 
7

1 단계 선언 :

private HttpContext currentContext; 
    static HttpContext _stCurrentContext; 

2 단계

currentContext = HttpContext.Current;  // in constructor 

3 단계

public override void ItemAdding(SPItemEventProperties properties) 
       _stCurrentContext = currentContext; 

4 단계

public override void ItemAdded(SPItemEventProperties properties) 
if (_stCurrentContext.Request.Files[0].ContentLength > 0) 
HttpPostedFile uploadfile = _stCurrentContext.Request.Files[0]; 
+1

Sudhakar에게 감사드립니다. 액세스 및 저장 HttpContext.Current ** 생성자 ** 내 가장 중요 한 것은 실종됐다. 다시 한번 감사드립니다. –

+0

이 버전은 한 사용자가 동시에 항목을 변경하는 한 계속 작동합니다. 다중 사용자 환경에서 경쟁 조건에 부딪 힐 가능성이 큽니다. –

0

당신은 내가 배치하면 그런 정적 변수에서, 당신은 또한 처음으로 이벤트 리시버를 실행 한 사용자의 컨텍스트가 될 동일한 컨텍스트 오브젝트를 사용하는 다수의 사용자가 있으며 동시에 변경하면 예기치 않은 결과가 발생할 수 있습니다.

사람들이 사용하지 않도록 문맥이 제거되었습니다. 나중에 호환성 문제를 피하기 위해 최대한 많이 노출 된 속성을 사용해보아야합니다. properties.Web.CurrentUser에서 사용자 이름을 얻을 수 있습니다.

이벤트 리시버에서 정적 변수를 사용하는 것이 까다로 우며 프런트 엔드가 여러 개인 경우 이벤트 리시버 인스턴스가 실행되는 프론트 엔드 외부에서 정적 변수의 데이터를 사용할 수 없습니다.

관련 문제