Showing posts with label SDL Tridion. Show all posts
Showing posts with label SDL Tridion. Show all posts

Aug 3, 2018

Add JS files to SDL Tridion CMS without a GUI element

I went through several blogs specially here. But still I found it difficult to do this simple task. I have created a step by step guide here in case if someone needs it.

1. Create a JS file. Sample file here from this link.
//If you only want your code to affect certain screens/views, you should listen to Anguilla events like this:
$evt.addEventHandler($display, "start", onDisplayStarted);

// This callback is called when any view has finished loading
function onDisplayStarted() {

$evt.removeEventHandler($display, "start", onDisplayStarted);

if ($display.getView().getId() == "ComponentView") {
        //Work your magic!
   }
}

2. Create a folder SampleModel at location: %TRIDION_HOME%\web\WebUI\Models and add the JS file to it. We will call the JS file as sample.js.

3. In the same folder create a config file as SampleModel.config as:
<?xml version="1.0"?>
<Configuration xmlns="http://www.sdltridion.com/2009/GUI/Configuration/Merge"
               xmlns:cfg="http://www.sdltridion.com/2009/GUI/Configuration">
  <resources cache="true">
    <cfg:filters/>
    <cfg:extensiongroups>
        <cfg:extensiongroup name="SampleModel.extension">
            <cfg:extension target="Tridion.Web.UI.Editors.CME.Views.Component">
            <cfg:insertafter>SampleModel</cfg:insertafter>
            </cfg:extension> 
        </cfg:extensiongroup>
    </cfg:extensiongroups>
    <cfg:groups>
      <cfg:group name="SampleModel" merger="Tridion.Web.UI.Editors.CME.Views.Component" merge="never" include="always">
        <cfg:fileset>
          <cfg:file type="script" id="sample">/sample.js</cfg:file>
        </cfg:fileset>
        <cfg:dependencies>
          <cfg:dependency></cfg:dependency>
        </cfg:dependencies>
      </cfg:group>
    </cfg:groups>
  </resources>
  <definitionfiles/>
  <extensions>
    <editorextensions xmlns="http://www.sdltridion.com/2009/GUI/extensions"/>
    <dataextenders xmlns="http://www.sdltridion.com/2009/GUI/extensions"/>
  </extensions>
  <commands/>
  <contextmenus/>
  <localization/>
  <settings>
    <defaultpage/>
    <navigatorurl/>
    <editurls/>
    <listdefinitions/>
    <itemicons/>
    <theme>
      <path/>
    </theme>
    <resourceextensions>
        <resourceextension>SampleModel.extension</resourceextension>
    </resourceextensions>
    <customconfiguration/>
  </settings>
</Configuration>

4. Modify the System.config file at location: %TRIDION_HOME%\web\WebUI\WebRoot\Configuration and add the following information under <models>:
<model name="SampleModel">
    <installpath>C:\Tridion\web\WebUI\Models\SampleModel\</installpath>
    <configuration>SampleModel.config</configuration>
    <vdir>SampleModel</vdir>
</model>

5. Open IIS and open the SDL Tridion site. Goto SDL Tridion -> WebUI -> Models and add a Virtual Directory. Give the path as: %TRIDION_HOME%\web\WebUI\Models\SampleModel

6. Restart IIS. If your code has no JS issues. The files should load in SDL Tridion CMS.



Nov 7, 2016

Iterating over string array in template in SDL Tridion 2013 SP1

Now we can add any string array to template package and iterate over its members in Dreamweaver template. We need to serialize your array and then create package string item out of it as mentioned below:

var list = new[] { "aaa", "bbb", "ccc" };           

StringWriter stringWriter = new StringWriter();
XmlSerializer xmlSerializer = new XmlSerializer(list.GetType());
xmlSerializer.Serialize(stringWriter, list); 

package.PushItem("ItemName",
                  package.CreateStringItem(ContentType.StringArray, stringWriter.ToString()));

The DWT for this array will be as:
  <!-- TemplateBeginRepeat name="ItemName" -->
    <div>@@Value@@</div>
  <!-- TemplateEndRepeat -->

Apr 24, 2015

SDL Tridion Core Service : Read All Components from a Publication based on Schema URI

The best way to fetch all components from a specific publication is by using the SearchQueryData class as:


SearchQueryData query = new SearchQueryData()
   {
          BasedOnSchemas = new BasedOnSchemaData[] { new BasedOnSchemaData() { Schema = new LinkToSchemaData() { IdRef = SchemaTcmUri } } },
          SearchIn = new LinkToIdentifiableObjectData() { IdRef = PublicationUri },
          ItemTypes = new ItemType[] { ItemType.Component }
   };
   try
   {
          XElement xResults = client.GetSearchResultsXml(query);
   }
   catch (System.ServiceModel.CommunicationException commProblem)
   {
          Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace);
          Console.Read();
   }

But, it requires the search to be properly configured in Tridion CMS. If you can search for an item based in Title/Text than most probably your code should work.

But if search is not properly configured, the code will throw the following error:


The remote server returned an error: (403) Forbidden.
Server stack trace:
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Tridion.ContentManager.CoreService.Client.ICoreService.GetSearchResultsXml(SearchQueryData filter)
   at Tridion.ContentManager.CoreService.Client.CoreServiceClient.GetSearchResultsXml(SearchQueryData filter)
A more detailed warning can be seen in Event Viewer


Error: The remote server returned an error: (403) Forbidden. 
Component: Tridion.ContentManager.CoreService Errorcode: 854 
User: server\tridion_admin 
StackTrace Information Details: System.Net.HttpWebRequest.GetResponse() 
Tridion.ContentManager.Search.SolrClient.ProcessResponse(HttpWebRequest,Boolean,Boolean,String&) 
Tridion.ContentManager.Search.SolrClient.Post(String,String,String&) 
Tridion.ContentManager.Search.SolrClient.Query(String,Int32,Nullable`1,String&) 
Tridion.ContentManager.Search.SearchQueryEngine.GetSearchResultsFromSolr(SearchQueryData,Int32,Nullable`1) 
Tridion.ContentManager.Search.SearchQueryEngine.GetSearchResultsFromSolr(SearchQueryData,Int32,Nullable`1) 
Tridion.ContentManager.Search.SearchQueryEngine.GetSearchResults(SearchQueryData,Int32) 
Tridion.ContentManager.Search.ComWrapper.SearchQueryEngineFacade.GetSearchResults(Int32,Int32) 
SearchBLST.GetListData SearchBLST.GetSearchResults 
at Tridion.ContentManager.ContentManagement.SearchQuery.GetListResults(Int32 startRowIndex, Int32 maxRows) 
at Tridion.ContentManager.Query.GetListResults() 
at Tridion.ContentManager.CoreService.CoreServiceBase.GetSearchResultsXml(SearchQueryData filter) 
at SyncInvokeGetSearchResultsXml(Object , Object[] , Object[] ) 
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) 
at Tridion.ContentManager.CoreService.TransactionSupportInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) 
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) 
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) 
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)  
CM Version: SDL Tridion 2011 SP 1


There is still a way to fetch List of TcmUri of related componets. It can be achieved through code:


UsingItemsFilterData usingComponentsFilter = new UsingItemsFilterData
{
    BaseColumns = ListBaseColumns.IdAndTitle, // to specify the detail in the XML
    ItemTypes = new[] { ItemType.Component },  // to specify certain items
    IncludedVersions = VersionCondition.OnlyLatestVersions //do not include old versions
};
IEnumerable<XElement> usingComponentXMLList = from comp in client.GetListXml(schemaTcmUri, usingComponentsFilter).Elements()
                                                select comp;
Hope this post helps others!!