0

I got the following error while running SharePoint ClientObject Model. I went through my code and checked if I missed any thing to load, but didn't see.

"Process is Terminated: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."

I am not sure what I am missing after searching about this for a couple of hour.

The following is my code. Hope any one can help.

    public static void UpdatePackageStatus(string Teamsite, string Libname, string Packagename, string User, string Password, string Domain, string PackageStatus, string DeploymentSucceeded, string query)
        {
            using(clientOM.ClientContext Ctx = new clientOM.ClientContext(Teamsite))
            {

                Ctx.Credentials = new System.Net.NetworkCredential(User, Password, Domain);
                clientOM.Web Web = Ctx.Web;
                Ctx.Load(Web);
                Ctx.ExecuteQuery();

                clientOM.List list = Web.Lists.GetByTitle(Libname);
                Ctx.Load(list);
                Ctx.ExecuteQuery();


                clientOM.CamlQuery CamlQuery = new clientOM.CamlQuery();
                CamlQuery.ViewXml = query;

                clientOM.ListItemCollection Items = list.GetItems(CamlQuery);
                Ctx.Load(Items);
                Ctx.ExecuteQuery();

                if(Items.Count > 0)
                {
                    clientOM.ListItem Item = Items.GetById(Items[0].Id);;
                    Ctx.Load(Item);
                    Ctx.ExecuteQuery();
                    if(Item.DisplayName == Packagename)
                    {
                    Item[PackageStatus] = DeploymentSucceeded; 
                    Item.Update(); 
                    Ctx.ExecuteQuery(); 
                    }
                }   
            }
        }
Sawlah
  • 1
  • 1
  • 1

2 Answers2

1

All used fields like DisplayName, Id and PackageStatus should be specified manually. You can use CAML query and write something like this:

string queryText = @"<View>
    <Query>
        <Where>
        <Eq>
            <FieldRef Name='{0}'/>
            <Value Type='Text'>{1}</Value>
        </Eq>
        </Where>
    </Query>
    </View>";
CamlQuery query = new CamlQuery();
query.ViewXml = string.Format(queryText, 'DisplayName', Packagename); //FieldRef = internal field name
ListItemCollection listItems = list.GetItems(query);
Ctx.Load(listItems, items => items.Include(item => item[PackageStatus]);
Ctx.ExecuteQuery();
if (listItems.Count > 0)
{
    listItems[0][PackageStatus] = DeploymentSucceeded;
    Item.Update(); 
    Ctx.ExecuteQuery();
}
zzeneg
  • 231
  • 1
  • 6
0

I see a fix.

Instead of using Item.DisplayName, I can use File object. It works when I use File and instantiating it by load(File) like other property.

    clientOM.File file = Item.File;
    Ctx.Load(file);
    Ctx.ExecuteQuery();

    if(file.Name == Packagename)
    {
    Item[PackageStatus] = DeploymentSucceeded; 
    Item.Update(); 
    Ctx.ExecuteQuery(); 
    }
Sawlah
  • 1
  • 1
  • 1