title: "RunWithElevatedPrivileges" in sharepoint
body: <p>"RunWithElevatedPrivileges": Programmatically in C# it doesn't help me to allow users <em>without</em> manage List permission to upload file to sharepoint list items. My code is:</p>
  
  <pre><code>SPSecurity.RunWithElevatedPrivileges(delegate
  {
      SPWeb web = SPContext.Current.Site;
  
      // my logic to upload file and edit list item attachments.
  });
  </code></pre>
  
  <p><strong>Complete code</strong></p>
  
  <pre><code>protected void btn_Upload_Click(object sender, EventArgs e)
      {
          StreamWriter sw = new StreamWriter(@"C:\Upload.txt", true);
          try
          {
              if (this.FileUpload1.HasFile)
              {
                  string siteURL = SPContext.Current.Web.Url.ToString();
                  if (Request["Items"] != null &amp;&amp; Request["ListId"] != null)
                  {
  
                      string SelectedItems = Convert.ToString(Request["Items"]);
                      string[] lstJobsIds = SelectedItems.Split(new string[] { "|" }, StringSplitOptions.None);
                      SPList list = null;
  
                      SPSecurity.RunWithElevatedPrivileges(delegate()
                      {
  
                          //SPSite site = SPContext.Current.Site;
                          using (SPSite site = new SPSite("http://sitrURL"))
                          {
                              using (SPWeb web = site.OpenWeb())
                              {
                                  // Fetch the List
                                  //list = web.Lists["ListName"];
                                  sw.WriteLine("WEb is :" + web);
                                  list = web.Lists["ListName"];
  
                                  if (lstJobsIds.Length &gt; 0)
                                  {
                                      ////site.AllowUnsafeUpdates = true;
  
                                      ////web.AllowUnsafeUpdates = true;
  
                                      for (int i = 0; i &lt; lstJobsIds.Length; i++)
                                      {
                                          // Get the List item
                                          if (lstJobsIds[i] != null &amp;&amp; lstJobsIds[i] != string.Empty)
                                          {
                                              sw.WriteLine(lstJobsIds[i]);
                                              SPListItem listItem = list.GetItemById(int.Parse(lstJobsIds[i]));
  
                                              // Get the Attachment collection
                                              SPAttachmentCollection attachmentCollection = listItem.Attachments;
                                              Stream attachmentStream;
                                              Byte[] attachmentContent;
                                              sw.WriteLine(this.FileUpload1.PostedFile);
                                              sw.WriteLine(this.FileUpload1.FileName);
  
                                              attachmentStream = this.FileUpload1.PostedFile.InputStream;
  
                                              attachmentContent = new Byte[attachmentStream.Length];
  
                                              attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);
  
                                              attachmentStream.Close();
                                              attachmentStream.Dispose();
  
                                              // Add the file to the attachment collection
                                              attachmentCollection.Add(this.FileUpload1.FileName, attachmentContent);
                                              // Update th list item
                                              listItem.Update();
  
                                              web.AllowUnsafeUpdates = true;
  
                                          }
                                      }
                                      //web.AllowUnsafeUpdates = false;
                                      //site.AllowUnsafeUpdates = false;
                                  }
  
  
                                  sw.Close();
  
                              }
                          }
                      });
                  }
  
              }
          }
          catch (Exception ex)
          {
              sw.WriteLine(ex);
              sw.Close();
          }
      }
  </code></pre>
  
  <p>Now When user click on button to upload file he gets <code>HTTP Error 403 Forbidden</code>.</p>
  
  <p>So,how to allow users With limit permission to execute my custom function normally?</p>