2

I currently have code that looks like this:

public static string GetListItemDisplayName(ClientContext clientContext, ListItem listItem)
    {
        string displayName;
        try
        {
            displayName = listItem.DisplayName;
        }
        catch
        {
            clientContext.Load(listItem, args => args.DisplayName);
            clientContext.ExecuteQuery();
            displayName = listItem.DisplayName;
        }
        return listItem.DisplayName;
    }

I have the feeling that using a trycatch this is not the most elegant solution to check if the ListItem DisplayName has been initiated. Is there any other way to do it? I have tried to use clientContext.Web.IsPropertyAvailable("DisplayName") but that doesn't seem to work the way I thought it would.

0

You can check if the property has been initialized by using the following code:

if (listItem.IsObjectPropertyInstantiated("DisplayName"))
{
//your code here
}

Here is the MSDN page that describes the property in details.

  • I have tried your solution, but the boolean always results in false on my end – vonis22 Dec 3 '18 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.