开发者

Using Sharepoint Server Object Model for Testing a Sharepoint Solution

I need to test a sharepoint solution my business has created. I have created a simple exe to be run on the actual sharepoint server.

I am trying to figure out how to use the Sharepoint Server OM to do my tests. At this point all I am trying to do is simply add an item to the library.

My first solution did something like this:

SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
etc...

The problem here is SPCOntext.Current.Site is always null.

My next attempt looked something like this:

SPSite site = new SPSite(url);
SPWeb web = si开发者_如何学JAVAte.OpenWeb();
SPList list = web.Lists[listName];
SPListItem item = list.AddItem();
item["Title"] = "Some Title";
item.Update();

This runs without any error, but when I check the list in question, the item I added isn't there.

Can anyone help guide me into where I am going wrong?


Try the following code:

using(SPSite site = new SPSite(url))
using(SPWeb web = site.OpenWeb())
{
    SPList list = web.Lists[listName];
    SPListItem item = list.AddItem();
    item["Title"] = "Some Title";

    web.AllowUnsafeUpdates = true;
    item.Update();
    list.Update();
    web.AllowUnsafeUpdates = false;
}

You may not need the AllowUnsafeUpdates (can't remember right now) but I'm almost positive you have to update the list as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜