Question

I have problems when I try to use SPListItem.

This is the code:

string URL = "http://vstkmy36773/Lists/Permissions/DispForm.aspx?ID=6&ContentTypeId=0x0100F385377F0CAD6C438A23B301CE04E7BF"

using (SPSite cSite = new SPSite(URL))
{
   using (SPWeb cWeb = cSite.OpenWeb())
   {
     // SPFile file = cWeb.GetFile(URL);
    //  SPListItem item = file.Item;
        SPListItem item = cWeb.GetListItem(URL);                            
        int id = item.ID;
        item["Title"] = id+ " update and get " + URL;
    }
 }

And the output

System.NullReferenceException: Object reference not set to an instance of an object. at Custom.Workflow.Activities.AddListItemPermissionAssigment.Execute(ActivityExecutionContext executionContext)

Was it helpful?

Solution

That's not the proper URL of the actual list item, from SharePoint's perspective. It's just the URL of some page that happens to display that item, which is different.

You're going to need to parse that URL, extract out the required information from it (namely the list and item ID), and then use that information to find the item:

var queryStrings = HttpUtility.ParseQueryString(url);
var listGuid = Guid.Parse(queryStrings["ListId"]);
var itemId = int.Parse(queryStrings["ID"]);

var item = web.Lists[listGuid].GetItemById(itemId);

If you're curious what the actual item URL is, print out the item.URL property to see what it actually is for that item. That's what your URL would need to contain for your code to actually work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top