Error: The Web application at http://...sitename.../ could not be - Ax |
12-Jun-07 07:10:01
|
Well, I was somehow able to obey the problem using the following code:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Opening sharepoint server at the relevant address
using (Site = new SPSite("http://...sitename..."))
{
// main code to work with sharepoint
}
});
The application at least starts and opens and I thought I won. However I was
happy too soon. It seems that there is no possible to insert, update and
delete lists items in this mode as the following code which works before
(when logged as administrator, otherwise I could not reach it) throws the
exceptions:
Guid listGuid = new
Guid("0f4ec74e-3338-4ff9-991b-1b8a83af56a7");
SPList list = lists[listGuid];
SPListItemCollection items = list.Items;
SPListItem listItem = items.Add();
rowNo++;
listItem[0] = idNo.Text;
listItem[1] = rowNo;
listItem[2] = description.Text;
listItem[3] = qtyNum;
listItem[4] = unit.SelectedValue;
listItem[5] = priceNum;
listItem[6] = currency.SelectedValue;
listItem.Update();
throws the excepion at the last line when Update() is applied:
System.InvalidOperationException: Operation is not valid due to the current
state of the object. at
Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext
context) +138
Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(HttpContext context)
+25 Microsoft.SharePoint.SPContext.get_Current() +106
Microsoft.SharePoint.SPListItem.GetValue(SPField fld, Int32 columnNumber,
Boolean bRaw) +440 Microsoft.SharePoint.SPListItem.GetValue(String strName,
Boolean bThrowException) +84 Microsoft.SharePoint.SPListItem.GetValue(String
strName) +32 Microsoft.SharePoint.SPListItem.InitItemID() +48
Microsoft.SharePoint.SPListItem.get_ID() +32
Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean
So I am where I was before. Any suggestions? Please help .... Thank you very
mych in advance for any information.
Roman |
 |
| |
Error: The Web application at http://...sitename.../ could not be - Steven Van de Craen |
12-Jun-07 07:21:53
|
The SPSite object will be disposed as soon as you exit the 'using' clause.
Any SPList or SPListItem objects that we're instantiated using the SPSite
object will be invalid. Make sure this isn't the case !
Steven |
 |
| |
Error: The Web application at http://...sitename.../ could not - Ax |
13-Jun-07 03:04:01
|
Hello Steven,
Thanks for the input but this is not the case. It is all inside 'using'
clause. The full script looks like this:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteCollection = new
SPSite("http://sharepoint"))
{
SPWebCollection sites = siteCollection.AllWebs;
SPWeb site = sites[0];
SPListCollection lists = site.Lists;
string userName =
HttpContext.Current.User.Identity.Name.ToString();
site.AllowUnsafeUpdates = true;
Guid listGuid = new
Guid("0f4ec74e-3338-4ff9-991b-1b8a83af56a7");
SPList list = lists[listGuid];
SPListItemCollection items = list.Items;
SPListItem listItem = items.Add();
rowNo++;
listItem[0] = idNo.Text;
listItem[1] = rowNo;
listItem[2] = description.Text;
listItem[3] = qtyNum;
listItem[4] = unit.SelectedValue;
listItem[5] = priceNum;
listItem[6] = currency.SelectedValue;
listItem.Update();
}
});
So it is inside. However the line listItem.Update() throws the exception
mentioned above.
Thanks,
Roman |
 |
| |
Error: The Web application at http://...sitename.../ could not be - Ax |
13-Jun-07 08:14:00
|
Hmmm, interesting. I have finally found the solution and it is quite easy. I
want almost cry when I realize that I've spent three days searching the
Internet and trying different solutions. I had to write maybe 20 MB of code
not believe it will work, but miraculously it does.
The only thing to do is to get SPSite in RunWithElevatedPrivileges relation
and then close RunWithElevatedPrivileges and the rest of the code to update
put behind that.
So the following code:
SPSite site = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
site = new SPSite("http://sharepointsrv"))
});
... some other code ....
Guid listGuid = new Guid("0f4ec74e-3338-4ff9-991b-1b8a83af56a7");
SPList list = lists[listGuid];
SPListItemCollection items = list.Items;
SPListItem listItem = items.Add();
rowNo++;
listItem[0] = idNo.Text;
listItem[1] = rowNo;
listItem[2] = description.Text;
listItem[3] = qtyNum;
listItem[4] = unit.SelectedValue;
listItem[5] = priceNum;
listItemDevil <img src="/> = currency.SelectedValue;
listItem.Update();
... works beautifully. The Udate comes through without throwing the
exception and everything is right. Wow, how easy. :) I am really narky that I
have spent so much time solving this but happy at the same time that it
works. I do not know which feeling dominates right now - well probably the
happiness. :)))
Roman |
 |
| |
Error: The Web application at http://...sitename.../ could not be - Jussi |
27-Jun-07 09:18:05
|
Thank you Axl, after so many hours trying to solve this problem I
finally managed to update my list item. |
 |
| |
Error: The Web application at http://...sitename.../ could not - Ax |
27-Jun-07 09:38:01
|
You are welcome. I am happy that it helped somebody because it was driving me
crazy and I could not find any solution on the web. I spent eternity on that
but finally found the solution and it is what counts. :)
Thanks also,
Roman |
 |
| |