vasanth
I'm new here

How to Store the Page (Content Store) reference in Table through API ??

Hello All,

I am using the FS_REFERENCE to refer the Content Store Page. After selecting the Page I want to store it in the Table through the FS API.

but i am getting the exception below

java.lang.ClassCastException: de.espirit.firstspirit.store.access.contentstore.Content2Impl cannot be cast to de.espirit.firstspirit.access.store.sitestore.PageRef

Please find code below

Template code

<FS_REFERENCE name="page" hFill="yes" sections="no" useLanguages="no">

    <FILTER>

      <ALLOW type="page"/>

    </FILTER>

    <LANGINFOS>

      <LANGINFO lang="*" label="XXX"/>

      <LANGINFO lang="DE" label="XXX"/>

    </LANGINFOS>

    <PROJECTS>

      <LOCAL name=".">

        <SOURCES>

          <FOLDER name="root" store="contentstore"/>

        </SOURCES>

      </LOCAL>

    </PROJECTS>

  </FS_REFERENCE>

FS API Code

Page page = (Page) element

FormField pagePageRefLinkFormField = pageEntityData.get(lang, Constants.content_page_reference);

TargetReference tRef = TargetReference.TargetReferences.newInstance(lang);

IDProvider provider = page.getStore().getStoreElement(page.getId());

tRef.set(provider);

pagePageRefLinkFormField.set(tRef);

Any Idea why I am getting this exception ??

Thank You,

Vasanth

0 Kudos
2 Replies
maaroufi
I'm new here

Dear Vasanth,

if you filter type page and only allow to choose from contentstore you won't be able to choose anything. Try it out by manually choosing from your 'page' component.

One example of how to store type page,pageref or content2 would be the following:

Template-Code:

   <FS_REFERENCE name="page" hFill="yes" sections="no" useLanguages="no">

        <FILTER>

          <ALLOW type="page"/>

          <ALLOW type="content2"/>

          <ALLOW type="pageref"/>

        </FILTER>

        <LANGINFOS>

          <LANGINFO lang="*" label="XXX"/>

        </LANGINFOS>

        <PROJECTS>

          <LOCAL name=".">

            <SOURCES>

              <FOLDER name="root" store="contentstore"/>

              <FOLDER name="root" store="pagestore"/>

              <FOLDER name="root" store="sitestore"/>

            </SOURCES>

          </LOCAL>

        </PROJECTS>

      </FS_REFERENCE>

Java-Code (without imports):

        UserService us2 = context.getUserService();

        PageStoreRoot pageStore = (PageStoreRoot) us2.getStore(

                Store.Type.PAGESTORE, false);

        ContentStoreRoot contentStore = (ContentStoreRoot) us2.getStore(

                Store.Type.CONTENTSTORE, false);

        SiteStoreRoot siteStore = (SiteStoreRoot) us2.getStore(

                Store.Type.SITESTORE, false);

        //The page containing your FS_REFERENCE

        Page page = (Page) pageStore.getStoreElement("mithras_home",

                UidType.PAGESTORE);

        FormData data = page.getFormData();

        //Getting FS_REFERENCE called 'page'

        FormField field =  data

                .get(us2.getProject().getMasterLanguage(), "page");

        PageRef ref = (PageRef) siteStore.getStoreElement(

                "productflash", UidType.SITESTORE_LEAF);

        Page p = (Page) pageStore.getStoreElement("aboutus_2",

                UidType.PAGESTORE);

        Content2 cont = (Content2) contentStore.getStoreElement("jobs",

                UidType.CONTENTSTORE);

        TargetReference t = field.get();

        page.setLock(true);

        //t.set(ref); //store a pagereference

        //t.set(p); //store a page

        t.set(cont); // store content2

        page.setFormData(data);

        page.save();

        page.setLock(false);

Yours sincerely

Ismail

0 Kudos
gockel
Crownpeak employee

Hi,

considering the recommended "Lock-Pattern" (see: Re: Medium mit Arbeitsablauf freigeben) the source code example above should bei optimized like this:

// .....

try {

     page.setLock(true, true);     // explicitly use recursive lock for pagestore pages

     //t.set(ref); //store a pagereference

     //t.set(p); //store a page

     t.set(cont); // store content2

     page.setFormData(data);

     page.save("content 2 set", true);          // we own a recursive lock -> explictly save recursive

} finally {

     // ensure to unlock the page in finally

     page.setLock(false, true);     // explicitly use recursive unlock for pagestore pages

}

0 Kudos