Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. SharePoint
  4. Throwing an exception when trying to attach a document to a list item

Throwing an exception when trying to attach a document to a list item

Scheduled Pinned Locked Moved SharePoint
databasequestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JimmyRopes
    wrote on last edited by
    #1

    I am throwing a System.ArgumentException when trying to attach a document to a list item. This is the code I am using. I had log statements (since removed to eliminate extraneous code) that showed me that I was getting to the

    Item.Attachments.AddNow(FileName, binFile);

    statement which was throwing the exception. The variable "FileName" is passed as a parameter of type String to the web service. The list has attachments enabled. Can anyone spot what I am doing wrong?

                // open prototype site and web
                site = new SPSite(SPContext.Current.Web.Url);
                web = site.OpenWeb("cabinet");
                SPSite oSite = null;
                SPWeb oWeb = null;
    
                if (0 < Id.Length
                 && 0 < FileName.Length)
                {
                    try
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            using (oSite = new SPSite(site.ID))
                            {
                                using (oWeb = oSite.OpenWeb(web.ID))
                                {
                                    oWeb.AllowUnsafeUpdates = true;
    
                                    SPQuery query = new SPQuery();
                                    query.Query = String.Concat(
                                        "<Where>",
                                                    "<Eq>",
                                                    "<FieldRef Name=\\"ItemNumber\\" />",
                                                    "<Value Type=\\"Number\\">" + Id + "</Value>",
                                                    "</Eq>",
                                        "</Where>");
                                    query.ViewFields = "<FieldRef Name=\\"ItemNumber\\" />";
                                    query.RowLimit = 1;
                                    SPListItemCollection listItems = oWeb.Lists\["ListName"\].GetItems(query);
                                    // perform if item returned
                                    foreach (SPListItem Item in listItems)
                                    {
                                        try
                                        {
    
    A 1 Reply Last reply
    0
    • J JimmyRopes

      I am throwing a System.ArgumentException when trying to attach a document to a list item. This is the code I am using. I had log statements (since removed to eliminate extraneous code) that showed me that I was getting to the

      Item.Attachments.AddNow(FileName, binFile);

      statement which was throwing the exception. The variable "FileName" is passed as a parameter of type String to the web service. The list has attachments enabled. Can anyone spot what I am doing wrong?

                  // open prototype site and web
                  site = new SPSite(SPContext.Current.Web.Url);
                  web = site.OpenWeb("cabinet");
                  SPSite oSite = null;
                  SPWeb oWeb = null;
      
                  if (0 < Id.Length
                   && 0 < FileName.Length)
                  {
                      try
                      {
                          SPSecurity.RunWithElevatedPrivileges(delegate()
                          {
                              using (oSite = new SPSite(site.ID))
                              {
                                  using (oWeb = oSite.OpenWeb(web.ID))
                                  {
                                      oWeb.AllowUnsafeUpdates = true;
      
                                      SPQuery query = new SPQuery();
                                      query.Query = String.Concat(
                                          "<Where>",
                                                      "<Eq>",
                                                      "<FieldRef Name=\\"ItemNumber\\" />",
                                                      "<Value Type=\\"Number\\">" + Id + "</Value>",
                                                      "</Eq>",
                                          "</Where>");
                                      query.ViewFields = "<FieldRef Name=\\"ItemNumber\\" />";
                                      query.RowLimit = 1;
                                      SPListItemCollection listItems = oWeb.Lists\["ListName"\].GetItems(query);
                                      // perform if item returned
                                      foreach (SPListItem Item in listItems)
                                      {
                                          try
                                          {
      
      A Offline
      A Offline
      Aptiva Dave
      wrote on last edited by
      #2

      I could never get the SPSecurity.RunWithElevatedPrivileges to work correctly when using webmethods. Have you tried this:

      //first, set the site to allow unsafe updates

      {
      SPWeb web = SPContext.Current.Web;
      web.AllowUnsafeUpdates = true;
      }

      J 2 Replies Last reply
      0
      • A Aptiva Dave

        I could never get the SPSecurity.RunWithElevatedPrivileges to work correctly when using webmethods. Have you tried this:

        //first, set the site to allow unsafe updates

        {
        SPWeb web = SPContext.Current.Web;
        web.AllowUnsafeUpdates = true;
        }

        J Offline
        J Offline
        JimmyRopes
        wrote on last edited by
        #3

        Aptiva Dave wrote:

        I could never get the SPSecurity.RunWithElevatedPrivileges to work correctly when using webmethods.

        I use it regurarly and have no problems with using it.

        Aptiva Dave wrote:

        Have you tried this:

        //first, set the site to allow unsafe updates

        {
        SPWeb web = SPContext.Current.Web;
        web.AllowUnsafeUpdates = true;
        }

        Yes it is in the code I posted.

        The report of my death was an exaggeration - Mark Twain
        Simply Elegant Designs JimmyRopes Designs
        Think inside the box! ProActive Secure Systems
        I'm on-line therefore I am. JimmyRopes

        1 Reply Last reply
        0
        • A Aptiva Dave

          I could never get the SPSecurity.RunWithElevatedPrivileges to work correctly when using webmethods. Have you tried this:

          //first, set the site to allow unsafe updates

          {
          SPWeb web = SPContext.Current.Web;
          web.AllowUnsafeUpdates = true;
          }

          J Offline
          J Offline
          JimmyRopes
          wrote on last edited by
          #4

          Aptiva Dave wrote:

          I could never get the SPSecurity.RunWithElevatedPrivileges to work correctly

          Notice how I set up site and web prototypes before going into the elevated permissions delegate. You may be running into trouble using SPContext.Current.Web.Url inside the delegate.

          The report of my death was an exaggeration - Mark Twain
          Simply Elegant Designs JimmyRopes Designs
          Think inside the box! ProActive Secure Systems
          I'm on-line therefore I am. JimmyRopes

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups