2012-12-10 2 views
1

팜 솔루션으로 배포 한 사용자 지정 SharePoint 웹 파트가 있고 SharePoint 목록에 쓰는 데 문제가 있습니다. 많은 다른 장소에서 AllowUnsafeUpdates를 True로 설정하고 SPListItem 업데이트를 호출하기 전에 SPWeb 개체를 업데이트하더라도 프로그래밍 방식으로 목록에 쓸 때 발생하는 오류 메시지는 다음과 같습니다.사용자 지정 웹 파트의 "GET 요청시 업데이트가 현재 허용되지 않습니다."

GET 요청에 대한 업데이트가 현재 허용되지 않습니다. . GET에서 업데이트를 허용하려면 SPWeb에서 'AllowUnsafeUpdates'속성을 설정하십시오.

나는 또한 RunWithElevatedPrivileges를 사용해 보았지만 행운은 없다.

 public void WriteError(string errorTask, string errorMessage) 
     { 
      string task = "Error Log Entry"; 
      string fileName = ""; 
      string fileTitle = ""; 
      string fileType = ""; 

       using (SPSite oSiteCollection = new SPSite(SPContext.Current.Web.Url)) 
       { 
        using (SPWeb oWebsite = oSiteCollection.RootWeb) 
        { 
         oSiteCollection.AllowUnsafeUpdates = true; 
         oWebsite.AllowUnsafeUpdates = true; 
         oWebsite.Update(); 

         SPSite errorLogSite = new SPSite(oWebsite.ServerRelativeUrl); 
         SPListItemCollection oList = errorLogSite.RootWeb.Lists["ErrorLog"].Items; 

         SPListItem oItem = oList.Add(); 
         oItem["ErrorTask"] = task + ": " + errorTask; 
         oItem["ErrorMessage"] = errorMessage; 
         oItem["UserName"] = String.IsNullOrEmpty(UserName) ? "Not Available" : UserName; 
         oItem["FileName"] = String.IsNullOrEmpty(fileName) ? "Not Available" : fileName; 
         oItem["Title"] = String.IsNullOrEmpty(fileTitle) ? "Not Available" : fileTitle; 
         oItem["FileType"] = String.IsNullOrEmpty(fileType) ? "Not Available" : fileType; 

         oItem.Update(); 
        } 
       } 

     } 
+0

관련없는,하지만 당신은 자신의 사이트 모음을 만들로 여기 해치지 않을 것입니다 ('spSiteCollection.RootWeb'을 폐기하지 않지만, 규칙이처럼하지 해); 또한'AllowUnsafeUpdates'는'SPWeb'의 _instance_에있는 속성이므로'Update'를 요구하지 않습니다. 하지만 Oleg는 귀하의 실제 질문에 대한 올바른 대답을 가지고 있다고 생각합니다. – Rawling

+0

이 웹 파트를 사용자 정의 페이지에서 호스팅하고 있습니까? –

답변

3

당신이 완벽하게 SPWeb의 AllowUnsafeUpdates 속성의 목표를 이해하지 못하는 것 같다 :

은 여기 참조 내 코드입니다. SPList 인스턴스 검색에 사용하는 SPWeb 인스턴스에서이 속성을 true로 설정해야합니다. 또한 성능을 저하시킬 수 있으므로 SPSite 및 SPWeb의 인스턴스를 너무 많이 만들 필요가 없습니다. 귀하의 경우를 들어 다음 코드 당신이

oSiteCollection.AllowUnsafeUpdates = true; 
oWebsite.AllowUnsafeUpdates = true; 

을하고

public void WriteError(string errorTask, string errorMessage) 
     { 
      string task = "Error Log Entry"; 
      string fileName = ""; 
      string fileTitle = ""; 
      string fileType = ""; 

      var errorLogWeb = SPContext.Current.Site.RootWeb; 
      errorLogWeb.AllowUnsafeUpdates = true; 
      var errorLogList = errorLogWeb.Lists["ErrorLog"]; 
      SPListItem oItem = errorLogList.Items.Add(); 
      oItem["ErrorTask"] = task + ": " + errorTask; 
      oItem["ErrorMessage"] = errorMessage; 
      oItem["UserName"] = String.IsNullOrEmpty(UserName) ? "Not Available" : UserName; 
      oItem["FileName"] = String.IsNullOrEmpty(fileName) ? "Not Available" : fileName; 
      oItem["Title"] = String.IsNullOrEmpty(fileTitle) ? "Not Available" : fileTitle; 
      oItem["FileType"] = String.IsNullOrEmpty(fileType) ? "Not Available" : fileType; 

      oItem.Update(); 
     } 
0

친구를 시도하지만 당신은

oSiteCollection.AllowUnsafeUpdates = false; 
    oWebsite.AllowUnsafeUpdates = false; 

,로를 설정하지 또한 당신이 그렇게 안 또 다른 한가지가 oSiteCollection.AllowUnsafeUpdates = false;입니다 .

이 코드를 시도

,

public void WriteError(string errorTask, string errorMessage) 
      { 
       string task = "Error Log Entry"; 
       string fileName = ""; 
       string fileTitle = ""; 
       string fileType = ""; 

        using (SPSite oSiteCollection = new SPSite(SPContext.Current.Web.Url)) 
        { 
         using (SPWeb oWebsite = oSiteCollection.RootWeb) 
         {       
          oWebsite.AllowUnsafeUpdates = true;    

          SPSite errorLogSite = new SPSite(oWebsite.ServerRelativeUrl); 
          SPListItemCollection oList = errorLogSite.RootWeb.Lists["ErrorLog"].Items; 

          SPListItem oItem = oList.Add(); 
          oItem["ErrorTask"] = task + ": " + errorTask; 
          oItem["ErrorMessage"] = errorMessage; 
          oItem["UserName"] = String.IsNullOrEmpty(UserName) ? "Not Available" : UserName; 
          oItem["FileName"] = String.IsNullOrEmpty(fileName) ? "Not Available" : fileName; 
          oItem["Title"] = String.IsNullOrEmpty(fileTitle) ? "Not Available" : fileTitle; 
          oItem["FileType"] = String.IsNullOrEmpty(fileType) ? "Not Available" : fileType; 

          oItem.Update(); 
          oWebsite.Update(); 
          oWebsite.AllowUnsafeUpdates = false; 
         } 
        } 

      } 
관련 문제