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
csharpdatabasesysadminquestion
1 Posts 1 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 in (since removed to cut down on extraneous code) that show me that I am getting up to the <pre>Item.Attachments.AddNow(FileName, binFile);</pre> statement when the exception is being thrown.

    FileName is a String passed as a parameter to the web service.

    Can anyone spot what I am doing wrong?

    <pre lang="c#">
    // open prototype site and web
    site = new SPSite(SPContext.Current.Web.Url);
    web = site.OpenWeb("cabinet");

                SPSite oSite = null;
                SPWeb oWeb = null;
    
    
                    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 = String.Concat("<FieldRef Name=\\"ItemNumber\\" />");
                                    query.RowLimit = 1;
                                    SPListItemCollection listItems = oWeb.Lists\["ListName"\].GetItems(query);
                                    // perform if item returned
                                    foreach (SPListItem Item in listItems)
                                    {
                                        try
                                        {
                                        String FilePath = String.Format("{0}{1}", Server.MapPath("~/\_doc\_
    
    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