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.