0

I have the following code below. The first time I run this code it doesnt create the property in the propertybag. The second time it will create the property. Why???

string newValue = new JavaScriptSerializer().Serialize(members);
            string currentValue = web.AllProperties[propertyName] as string;

            if (!newValue.Equals(currentValue))
            {
                web.AllProperties[propertyName] = newValue;
                needsUpdate = true;
            }

            if (!web.IndexedPropertyKeys.Contains(propertyName))
            {
                web.IndexedPropertyKeys.Add(propertyName);
                needsUpdate = true;
            }

            if (needsUpdate)
            {
                web.Update();
            }

UPDATE:

if (!web.IndexedPropertyKeys.Contains(propertyName))
            {
                web.IndexedPropertyKeys.Add(propertyName);
                needsUpdate = true;
            }

if (!newValue.Equals(currentValue))
            {
                web.AllProperties[propertyName] = newValue;
                needsUpdate = true;
            }

            if (needsUpdate)
            {
                web.Update();
            }

1 Answer 1

0

Please add the property before you update it. It is creating the property in the first run and updating the value in the second run.

2
  • see my update. Like this?
    – Ola
    Commented Sep 10, 2015 at 13:50
  • Yes, is it working?
    – Vamsi K K
    Commented Sep 10, 2015 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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