Click here to Skip to main content
12,999,867 members (48,079 online)

SharePoint

 
AnswerRe: Getting an error when trying to upload a document Pin
ihoecken30-Jan-13 2:57
memberihoecken30-Jan-13 2:57 
GeneralRe: Getting an error when trying to upload a document Pin
JimmyRopes30-Jan-13 4:05
memberJimmyRopes30-Jan-13 4:05 
QuestionRequest for Sharepoint tutorial links Pin
obalesu28-Jan-13 18:18
memberobalesu28-Jan-13 18:18 
AnswerRe: Request for Sharepoint tutorial links Pin
ihoecken28-Jan-13 21:13
memberihoecken28-Jan-13 21:13 
QuestionCan CAML query handle multiple lists? Pin
Clark Kent12328-Jan-13 5:35
memberClark Kent12328-Jan-13 5:35 
AnswerRe: Can CAML query handle multiple lists? Pin
ihoecken28-Jan-13 21:17
memberihoecken28-Jan-13 21:17 
GeneralRe: Can CAML query handle multiple lists? Pin
Clark Kent12329-Jan-13 1:34
memberClark Kent12329-Jan-13 1:34 
QuestionThrowing an exception when trying to attach a document to a list item Pin
JimmyRopes25-Jan-13 2:34
memberJimmyRopes25-Jan-13 2:34 
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
                                        {
                                        String FilePath = String.Format("{0}{1}", Server.MapPath("~/_doc_uploads/"), FileName);
                                            FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
                                            byte[] binFile = new byte[fs.Length];
                                            fs.Read(binFile, 0, System.Convert.ToInt32(fs.Length));
                                            fs.Close();
                                            fs.Dispose();
                                            Item.Attachments.AddNow(FileName, binFile);
                                            Item.Update();
                                        } // try
                                        catch (Exception ex)
                                        {
                                            Log.WriteExceptionInfo(ex);
                                        }
                                    } // foreach (SPListItem Item in listItems)
                                    oWeb.AllowUnsafeUpdates = false;
                                } // using (SPWeb oWeb = oSite.OpenWeb(web.ID))
                            } // using (SPSite oSite = new SPSite(site.ID))
                        }); // SPSecurity.RunWithElevatedPrivileges(delegate()
                        returnData.Status = 0;
                        returnData.Id = Id;
                    } // try
                    catch (Exception ex)
                    {
                        returnData.Status = 30;
                        returnData.Msg = String.Format("{0}\n\nContact technical support and inform them of the error you have received.", ex.Message);
                        Log.WriteLine(ex.Message);
                        Log.WriteExceptionInfo(ex);
                    } // catch (Exception ex)
                    finally
                    {
                        if (oWeb != null) oWeb.Dispose();
                        if (oSite != null) oSite.Dispose();
                        if (web != null) web.Dispose();
                        if (site != null) site.Dispose();
                    } // finally
                } // if (0 < Id.Length etc.


This is the exception that is thrown.

1/24/2013 14:20:33.3051534 - --------- Exception Data ---------
Message: Value does not fall within the expected range.
Exception Type: System.ArgumentException
Source: Microsoft.SharePoint
StrackTrace:    at Microsoft.SharePoint.SPFieldMap.GetColumnNumber(String strFieldName)
   at Microsoft.SharePoint.SPListItemCollection.GetRawValue(String fieldname, Int32 iIndex)
   at Microsoft.SharePoint.SPListItem.GetValue(SPField fld, Int32 columnNumber, Boolean bRaw)
   at Microsoft.SharePoint.SPListItem.GetValue(String strName, Boolean bThrowException)
   at Microsoft.SharePoint.SPListItem.GetValue(String strName)
   at Microsoft.SharePoint.SPListItem.get_Attachments()
   at nsDashboardSpServices.DashboardSpServices.<>c__DisplayClass82.<addLoaAttachment>b__7e()
TargetSite: Int32 GetColumnNumber(System.String)

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



AnswerRe: Throwing an exception when trying to attach a document to a list item Pin
Aptiva Dave31-Jan-13 5:07
memberAptiva Dave31-Jan-13 5:07 
GeneralRe: Throwing an exception when trying to attach a document to a list item Pin
JimmyRopes31-Jan-13 13:30
memberJimmyRopes31-Jan-13 13:30 
GeneralRe: Throwing an exception when trying to attach a document to a list item Pin
JimmyRopes7-Feb-13 2:31
memberJimmyRopes7-Feb-13 2:31 
QuestionHo to Enable Titlle Column in Document Library using power shell in sharepoint 2010 Pin
ps4124-Jan-13 18:21
memberps4124-Jan-13 18:21 
AnswerRe: Ho to Enable Titlle Column in Document Library using power shell in sharepoint 2010 Pin
ihoecken24-Jan-13 22:06
memberihoecken24-Jan-13 22:06 
QuestionHow to intergrade Sarepoint contacts with MS exchange server? Pin
cns connect20-Jan-13 21:57
membercns connect20-Jan-13 21:57 
AnswerRe: How to intergrade Sarepoint contacts with MS exchange server? Pin
ihoecken20-Jan-13 23:44
memberihoecken20-Jan-13 23:44 
QuestionExternal content type crawlling sharepoint 2013 Pin
Member 401525120-Jan-13 8:40
memberMember 401525120-Jan-13 8:40 
Questionsharepoint 2010 birthday reminder webpart Pin
ssivarao17-Jan-13 1:13
memberssivarao17-Jan-13 1:13 
AnswerRe: sharepoint 2010 birthday reminder webpart Pin
ihoecken20-Jan-13 23:42
memberihoecken20-Jan-13 23:42 
Questionform Infopath Pin
zohre yousefi16-Jan-13 20:12
memberzohre yousefi16-Jan-13 20:12 
AnswerRe: form Infopath Pin
ihoecken16-Jan-13 23:24
memberihoecken16-Jan-13 23:24 
QuestionHow do I add a drop down list to the New Item... Pin
Clark Kent12315-Jan-13 8:50
memberClark Kent12315-Jan-13 8:50 
AnswerRe: How do I add a drop down list to the New Item... Pin
Clark Kent12316-Jan-13 7:06
memberClark Kent12316-Jan-13 7:06 
QuestionCannot connect to business data connectivity Metadata store Sharepoint designer 2013 Pin
Member 401525113-Jan-13 18:56
memberMember 401525113-Jan-13 18:56 
AnswerRe: Cannot connect to business data connectivity Metadata store Sharepoint designer 2013 Pin
Clark Kent12315-Jan-13 8:42
memberClark Kent12315-Jan-13 8:42 
GeneralRe: Cannot connect to business data connectivity Metadata store Sharepoint designer 2013 Pin
Member 401525115-Jan-13 22:19
memberMember 401525115-Jan-13 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.8.170624.1 | Last Updated 10 May 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid