Get Element by GUID?

31 views
Skip to first unread message

suzan...@gmail.com

unread,
Jul 5, 2017, 3:49:20 PM7/5/17
to SmartAPI
Hello,

I am looking to build an application that will essentially update a placeholder value on a Red Dot page with Smart API.

I am having trouble finding a function that will get a placeholder element by its GUID in order to update the placeholder value.

Any thoughts surrounding this approach?

Jonas Jacobi

unread,
Jul 5, 2017, 6:30:03 PM7/5/17
to SmartAPI

Hi,

you are looking for

PageElementFactory.Instance.CreateElement

http://www.smartapi.de/smartapi_doc/a00253.html#ad37a665258f62ee68a8a97043ff2dd4e


You then need to cast the return value to the appropriate element type.


Hope this helps

Jonas

Collaboration diagram for erminas.SmartAPI.CMS.Project.Pages.Elements.PageElementFactory:



Von: smar...@googlegroups.com <smar...@googlegroups.com> im Auftrag von suzan...@gmail.com <suzan...@gmail.com>
Gesendet: Mittwoch, 5. Juli 2017 21:49:20
An: SmartAPI
Betreff: [SmartAPI] Get Element by GUID?
 
--
More information about SmartAPI: http://www.smartapi.de
---
Sie erhalten diese Nachricht, weil Sie in Google Groups E-Mails von der Gruppe "SmartAPI" abonniert haben.
Wenn Sie sich von dieser Gruppe abmelden und keine E-Mails mehr von dieser Gruppe erhalten möchten, senden Sie eine E-Mail an smartapi+u...@googlegroups.com.
Wenn Sie in dieser Gruppe einen Beitrag posten möchten, senden Sie eine E-Mail an smar...@googlegroups.com.
Weitere Optionen finden Sie unter https://groups.google.com/d/optout.

suzan...@gmail.com

unread,
Jul 6, 2017, 9:43:24 AM7/6/17
to SmartAPI
Hi Jonas,

I already have the element created. Do you happen to have an example line of code where I can assign the value to the element guid?

Thank you,

Suzanne

suzan...@gmail.com

unread,
Jul 6, 2017, 1:40:51 PM7/6/17
to SmartAPI
Hi Jonas,

I was able to retrieve the element with this line of code:

var getElement = erminas.SmartAPI.CMS.Project.Pages.Elements.PageElementFactory.Instance.CreateElement(selectedProject, elementGuid, languageVariant);


Say if I want to update the value of this element (update the text to 'Hello World') which line of code should I be using?

Thank you!

Suzanne

suzan...@gmail.com

unread,
Jul 6, 2017, 4:41:21 PM7/6/17
to SmartAPI
The element type I am targeting is TextHTML.

Hilmar Bunjes

unread,
Jul 6, 2017, 5:14:55 PM7/6/17
to smar...@googlegroups.com
Hi,
You can cast in the element to IText (in the Pages namespace), set the property Value and then call Commit(). 

Best
Hilmar

Sent via mobile phone

Am 06.07.2017 um 22:41 schrieb "suzan...@gmail.com" <suzan...@gmail.com>:

The element type I am targeting is TextHTML.

--

suzan...@gmail.com

unread,
Jul 6, 2017, 5:32:29 PM7/6/17
to SmartAPI

Hi Hilmar,

Do you happen to have a code snippet of this?

Thank you!

Suzanne

Jonas Jacobi

unread,
Jul 6, 2017, 6:31:46 PM7/6/17
to SmartAPI

var element = (ITextHtml)erminas.SmartAPI.CMS.Project.Pages.Elements.PageElementFactory.Instance.CreateElement(selectedProject, elementGuid, languageVariant); //you could also cast to IText as Hilmar said. It's a super class of ITextHtml

element.Value = "<p>my new text</p>";

element.Commit();


Cheers

Jonas


Gesendet: Donnerstag, 6. Juli 2017 23:32:29
An: SmartAPI
Betreff: Re: [SmartAPI] Get Element by GUID?
 

suzan...@gmail.com

unread,
Jul 7, 2017, 9:47:09 AM7/7/17
to SmartAPI
Thanks Jonas.

I'm getting the following errors:

Severity

Code

Description





Error

CS1061

'IPageElement' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'IPageElement' could be found (are you missing a using directive or an assembly reference?)





Error

CS1061

'IPageElement' does not contain a definition for 'Commit' and no extension method 'Commit' accepting a first argument of type 'IPageElement' could be found (are you missing a using directive or an assembly reference?)






I installed SMARTAPI from the NuGet manager - am I missing any steps?

Hilmar Bunjes

unread,
Jul 7, 2017, 9:54:39 AM7/7/17
to smar...@googlegroups.com
Suzanne,
this cast is missing on your side:

var element = (ITextHtml)erminas.SmartAPI.CMS.Project.Pages.Elements.PageElementFactory.Instance.CreateElement(selectedProject, elementGuid, languageVariant);

Can you show the source code after you get the element with PageElementFactory.Instance.CreateElement ?


Thanks,
Hilmar

suzan...@gmail.com

unread,
Jul 7, 2017, 10:01:21 AM7/7/17
to SmartAPI
Hi Hilmar,

I am getting this error after applying the cast

Severity

Code

Description





Error

CS0246

The type or namespace name 'ITextHtml' could not be found (are you missing a using directive or an assembly reference?)







using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using erminas.SmartAPI.CMS.Project;
using erminas.SmartAPI.CMS.ServerManagement;
using System.Net;
using erminas.SmartAPI.Utils;
using erminas.SmartAPI.CMS;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var authData = new PasswordAuthentication("", "");
        var url = "";
        var login = new ServerLogin(url, authData);
        using (var session = SessionBuilder.CreateOrReplaceOldestSession(login))
        {

            var selectedProject = session.SelectedProject;
            var serverManager = session.ServerManager;

            var languageVariant = selectedProject.LanguageVariants.Current;
            var pageGuid = new Guid("");
            var elementGuid = new Guid("");

            var getElement = (ITextHtml)erminas.SmartAPI.CMS.Project.Pages.Elements.PageElementFactory.Instance.CreateElement(selectedProject, elementGuid, languageVariant);
            getElement.Value = "<p>my new text</p>";
            getElement.Commit();

Hilmar Bunjes

unread,
Jul 7, 2017, 10:03:52 AM7/7/17
to smar...@googlegroups.com
Suzanne,
ITextHtml is in the namespace "erminas.SmartAPI.CMS.Project.ContentClasses.Elements". If you import this namespace it should work.

Best,
Hilmar

suzan...@gmail.com

unread,
Jul 7, 2017, 10:09:00 AM7/7/17
to SmartAPI
I am getting these errors now after importing the namespace -

Severity

Code

Description





Error

CS1061

'ITextHtml' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'ITextHtml' could be found (are you missing a using directive or an assembly reference?)





Error

CS1061

'ITextHtml' does not contain a definition for 'Commit' and no extension method 'Commit' accepting a first argument of type 'ITextHtml' could be found (are you missing a using directive or an assembly reference?)





Hilmar Bunjes

unread,
Jul 7, 2017, 10:12:25 AM7/7/17
to smar...@googlegroups.com
Suzanne,
sorry, my fault. You need this namespace: "erminas.SmartAPI.CMS.Project.Pages.Elements.ITextHtml" instead of the one I mentioned. The other one is for CC elements and not page elements. Of course, you can only set a value for a page element.

Best
Hilmar

suzan...@gmail.com

unread,
Jul 7, 2017, 10:22:12 AM7/7/17
to SmartAPI
This works perfectly! Thank you so much.

That being said, do you happen to know how I could pull the user's currently selected page and element? I know there is a function for selected project but I am wondering if there is an equivalent for selected page as well as selected element?

Thank you,

Suzanne

Hilmar Bunjes

unread,
Jul 7, 2017, 11:29:26 AM7/7/17
to smar...@googlegroups.com
Suzanne,
you are very welcome.

You can get the current project via SmartAPI but not the current page or page element, unfortunately. However, this content is stored in the ASP session of the user. If you open your webpage from SmartTree you'd need an ASP page that reads the session vars and redirect to the ASP.NET page that uses them with the values as GET params for example.

The ASP page would contain such a code (to forward all session vars):
<%
Dim sActionPageURL, sContent
 
sActionPageURL = "http://localhost/cms/plugins/session_store.aspx"
sContent = "?"
 
For Each Item In Session.Contents
 sContent = sContent & Item & "=" & server.URLencode(Session.Contents(item)) & "&"
Next

Response.Redirect sActionPageURL & sContent
 
%>

Best
Hilmar
Reply all
Reply to author
Forward
0 new messages