1

I am new to Core Service and I want to create new redirect Components inside the "Redirects" folder of CMS.

I have gone through many ques and code but couldn't find a way to create Redirect Components and set the values of mandatory fields inside it. Can someone please assist with this?

2
  • There are many articles explained about to create a component using tridion core service? I'm not sure which one you have looked at not helped, can you please share what you have done so far code reference or error?
    – Velmurugan
    Commented Jan 21, 2019 at 18:48
  • The below answer helped you to solve the problem, please upvote and accept the answer to close this ticket thread?
    – Velmurugan
    Commented May 16, 2019 at 12:34

1 Answer 1

3

Sample code for creating a component using Tridion CoreServices

tcm:12-1147-2 - Replace this Id with your Redirects folder Id

tcm:12-1607-8 - Replace this Id with your redirect schema Id

You need to modify to add your component content and make sure it's valid according to schema fields and namespace Id.

using System;
using System.ServiceModel;
using System.Text;
using Tridion.ContentManager.CoreService.Client;

namespace Tridion.CoreService.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ICoreService client = GetClient("localhost", @"domain\username", "password");

                Console.WriteLine("Connected to CoreService with user " + client.GetCurrentUser().Title);


                ReadOptions ro = new ReadOptions { LoadFlags = LoadFlags.None };
                var componentData = client.GetDefaultData(ItemType.Component, "tcm:12-1147-2", ro) as ComponentData;
                componentData.Title = "Redirect Component 02";
                componentData.Schema = new LinkToSchemaData() { IdRef = "tcm:12-1607-8" };

                StringBuilder content = new StringBuilder();
                content.Append("<Content xmlns=\"uuid:cd9aae9e-2240-480e-a772-ae1010e88357\">");
                content.Append("<url xmlns=\"uuid:cd9aae9e-2240-480e-a772-ae1010e88357\">/en/contactus</url>");
                content.Append("</Content>");

                componentData.Content = content.ToString();

                var newComponent = client.Create(componentData, ro) as ComponentData;

                Console.WriteLine("Component Created with Id: {0}", newComponent.Id);
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine("CreateComponent: Unable to create a component {0}", ex.Message);
            }
        }

        static ICoreService GetClient(string hostname, string username, string password)
        {
            var binding = new BasicHttpsBinding()
            {
                MaxBufferSize = 4194304, // 4MB
                MaxBufferPoolSize = 4194304,
                MaxReceivedMessageSize = 4194304,
                ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
                {
                    MaxStringContentLength = 4194304, // 4MB
                    MaxArrayLength = 4194304,
                },
                Security = new BasicHttpsSecurity()
                {
                    Mode = BasicHttpsSecurityMode.Transport,
                    Transport = new HttpTransportSecurity()
                    {
                        ClientCredentialType = HttpClientCredentialType.Basic
                    }
                }
            };

            hostname = string.Format("{0}{1}{2}", hostname.StartsWith("https") ? "" : "https://", hostname, hostname.EndsWith("/") ? "" : "/");
            var endpoint = new EndpointAddress(hostname + "webservices/CoreService201603.svc/basicHttp");
            var factory = new ChannelFactory<ICoreService>(binding, endpoint);


            var credentialBehaviour = factory.Endpoint.Behaviors.Find<System.ServiceModel.Description.ClientCredentials>();
            credentialBehaviour.UserName.UserName = username;
            credentialBehaviour.UserName.Password = password;
            return factory.CreateChannel();
        }

    }
}

I hope it helps.

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.