Throwing an exception when trying to attach a document to a list item
-
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 {
-
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 {
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;
} -
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;
}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 -
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;
}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