0

I am interating over SharePoint Fields to examine the Hidden Property (Sytem.Boolean) so it can be toggled. I am noticing that the GetValue(f,null) is always True even when I know the field Hidden property is False. I don't see why it keeps returning true. Thanks

var list = ctx.Web.Lists.GetById(libGuid);
var fields = list.Fields;
ctx.Load(list);
ctx.Load(fields);
ctx.ExecuteQuery();

List<object> fieldPropList = new List<object>();

foreach (Field f in fields)
 {
    List<PropertyInfo> props = f.GetType().GetProperties().ToList();
    foreach (var prop in props)
    {
        if (prop.Name == "Hidden")
        {
            fieldPropList.Add(new
            {
                PropertyName = prop.Name,
                PropertyType = prop.PropertyType.ToString(),
                CanRead = prop.CanRead,
                CanWrite = prop.CanWrite,
                Value = prop.GetValue(f, null).ToString()  // Always TRUE why?
            });
        }
    }
ChiliYago
  • 11,341
  • 23
  • 79
  • 126
  • shouldn't `PropertyInfo` be an array like this for example `PropertyInfo[] props` I would change to this `PropertyInfo[] props = f.GetType().GetProperties();` then I would do `foreach(var info in props)` – MethodMan Feb 06 '15 at 19:49
  • I can post you an example of a Method I wrote that will convert `null` values of a `Class` when you initialize it using the new Construct. to make the fields = 'string.Empty` upon Initialization in theory you should be able to get at the `Name` since it's one of the Properties. let me know if you want to see my example.. also shouldn't this line `Value = prop.GetValue(f, null).ToString()` be like this - `Value = prop.GetValue(f, null)` – MethodMan Feb 06 '15 at 19:54
  • I'd like to see it. So I can mark this as answered – ChiliYago Feb 06 '15 at 20:50
  • my example woks off a class you can get the gist based on that .. it would be nice how ever to see the full class that you have so that it would be easier to make edits to as well – MethodMan Feb 06 '15 at 21:51

2 Answers2

0

Pretty sure you need to load the field also...

var list = ctx.Web.Lists.GetById(libGuid);
var fields = list.Fields;
ctx.Load(list);
ctx.Load(fields);
ctx.ExecuteQuery();

List<object> fieldPropList = new List<object>();

foreach (Field f in fields)
 {
    ctx.Load(f);   //  <<== *** LOAD THE FIELD ***
    List<PropertyInfo> props = f.GetType().GetProperties().ToList();
    foreach (var prop in props)
    {
    if (prop.Name == "Hidden")
    {
        fieldPropList.Add(new
        {
        PropertyName = prop.Name,
        PropertyType = prop.PropertyType.ToString(),
        CanRead = prop.CanRead,
        CanWrite = prop.CanWrite,
        Value = prop.GetValue(f, null).ToString()  // Always TRUE why?
        });
    }
    }
MethodMan
  • 18,625
  • 6
  • 34
  • 52
Madison
  • 411
  • 5
  • 14
  • Load(fields) is the third line in my code so I am pretty sure I have them loaded. The load you have does not have an ExecuteQuery() – ChiliYago Feb 06 '15 at 21:04
  • csom is the worst, am I right? anyway, you are loading the list...what I'm saying is I think you need to load each individual field also so it's values get populated. hard to tell without being able to run the code, but I'm fairly certain that's how I've gotten around this before. Hope it works for you! – Madison Feb 06 '15 at 21:07
0

here is an example if you were to work the code below off of a Class - per your Request

public static void ConvertNullToStringEmpty<T>(this T clsObject) where T : class
{
    PropertyInfo[] properties = clsObject.GetType().GetProperties();
    foreach (var info in properties)
    {
        // if a string and null, set to String.Empty
        if (info.PropertyType == typeof(string) && info.GetValue(clsObject, null) == null)
        {
            info.SetValue(clsObject, String.Empty, null);
        }
    }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52