7

I am trying to update the Target Groups conditions in the component presentations of a page using the Core Service. I am in SDL Tridion 2013 and here is the code I am using:

try
{
    OpenSession(serviceInput.Server, serviceInput.User);
    page = session.Read(context.PageUri, new ReadOptions()) as PageData;
    if (page!=null)
    {
        page = session.CheckOut(context.PageUri, true, new ReadOptions()) as PageData;
        List<TargetGroupConditionData> tgs = new List<TargetGroupConditionData>();
        foreach (ComponentPresentationData cp in page.ComponentPresentations)
        {
            if (cp.Component.IdRef.Equals(context.ComponentUri) &&
                cp.ComponentTemplate.IdRef.Equals(context.TemplateUri))
            {
                foreach (MobileTargetGroupInput input in groups)
                {
                    TargetGroupConditionData data = new TargetGroupConditionData()
                    {
                        Negate = input.Negate,
                        TargetGroup = new LinkToTargetGroupData() {IdRef = input.Uri}
                    };
                    tgs.Add(data);
                }
                cp.Conditions = tgs.ToArray();
            }
        }

        session.CheckIn(context.PageUri,true,string.Empty,null);

        result.Success = true;
        result.Message = String.Format("Page {0} updated.", context.PageUri);
    }

    return result;
}
catch (Exception ex)
{
    result.Success = false;
    result.Message = ex.Message;
    return result;
}
finally
{
    CloseSession();
}

The code does not fail at all, but the page in question, never gets updated. Can you guys help me out? I don't understand what I am missing.

7

I figured it out:

I was missing the .Save call before the .CheckIn. So the code should be like this:

try
{
    OpenSession(serviceInput.Server, serviceInput.User);
    page = session.Read(context.PageUri, new ReadOptions()) as PageData;
    if (page != null)
    {
        page = session.CheckOut(context.PageUri, true, new ReadOptions()) as PageData;
        List<TargetGroupConditionData> tgs = new List<TargetGroupConditionData>();
        foreach (ComponentPresentationData cp in page.ComponentPresentations)
        {
            if (cp.Component.IdRef.Equals(context.ComponentUri) &&
                cp.ComponentTemplate.IdRef.Equals(context.TemplateUri))
            {
                foreach (MobileTargetGroupInput input in groups)
                {
                    TargetGroupConditionData data = new TargetGroupConditionData()
                    {
                        Negate = input.Negate,
                        TargetGroup = new LinkToTargetGroupData() {IdRef = input.Uri}
                    };
                    tgs.Add(data);
                }
                cp.Conditions = tgs.ToArray();
            }
        }  

        // I WAS MISSING THIS LINE!
        session.Save(page, null);

        session.CheckIn(context.PageUri,true,string.Empty,null);

        result.Success = true;
        result.Message = String.Format("Page {0} updated.", context.PageUri);
    }

    return result;
}
catch (Exception ex)
{
    result.Success = false;
    result.Message = ex.Message;
    return result;
}
finally
{
    CloseSession();
}
  • 1
    DUH! (facepalm) – Nuno Linhares Nov 6 '13 at 14:20
  • @Bart, you are so into look & feel, I think it was clearer before ;) – Jaime Santos Alcón Nov 6 '13 at 15:14
  • @Jaime, I found it unreadable as it was, but you can always undo my edit of course ;) – Bart Koopman Nov 6 '13 at 15:17
  • 1
    actually, you are missing spaces around condition "page!=null". It should read "page != null"... that would make it much clearer :) – Mihai Cădariu Nov 6 '13 at 15:19
  • 1
    @Jaime, be glad I didn't make the comment say // IN SPAIN YOU DON'T HAVE TO DO THIS LINE – Bart Koopman Nov 6 '13 at 15:50

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.