public static string GetProductTcmId(CoreService2010Client client, string productId) { string productTcmId = ""; try { //get the XML list of component from the folder var productsXML = client.GetListXml(ConfigurationManager.AppSettings["ProductFolderTcmId"], new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Component } }); //loop through each item and find out if it is the product we want foreach (var product in productsXML.Elements()) { var productData = client.Read(product.Attribute("ID").Value, null) as ComponentData; var schemaFields = client.ReadSchemaFields(productData.Schema.IdRef, false, null); var content = XDocument.Parse(productData.Content); XNamespace ns = schemaFields.NamespaceUri; //check if the product id's match if (productId == content.Root.Element(ns + "product_id").Value) { //return the TcmId productTcmId = product.Attribute("ID").Value; //exit the foreach break; } } } catch (Exception ex) { Log.Error("Error in GetProductTcmId()", ex); } return productTcmId; }
protected void btnUpload_Click(object sender, EventArgs e) { // Check file has been selected if (!fileUpload.HasFile || string.IsNullOrEmpty(fileUpload.PostedFile.FileName)) { badMessage.Text = "Please select a file to upload."; badMessage.Visible = true; return; } // Check file has valid extension var fileExt = Path.GetExtension(fileUpload.PostedFile.FileName); if (string.IsNullOrEmpty(fileExt) || !ValidExtensions.IsMatch(fileExt)) { badMessage.Text = "The uploaded file has an invalid name or extension."; badMessage.Visible = true; return; } try { // Save upload to disk string virtualPath = "~\\Bin\\" + fileUpload.FileName; string absPath = Server.MapPath(virtualPath); Log.Info("save file to " + absPath); fileUpload.SaveAs(absPath); // Parse XLS Log.Debug("Start XLS read"); var productPrices = ReadXlsFile(virtualPath); int count = productPrices.Count; Log.Info("End XLS read " + count); using (var client = new CoreService2010Client()) { Log.Debug("Start processing pricing components"); CreateComponents(client, productPrices); Log.Debug("End processing pricing components"); } } catch (Exception ex) { Log.Error("PricingManagement.UploadFile: Error retrieving and setting XLS", ex); badMessage.Text = "An occurred while processing the product prices." + ex.StackTrace; badMessage.Visible = true; } }
public void CreateComponents(CoreService2010Client client, List <ProductPrice> productPrices) { try { int count = 1; foreach (var productPrice in productPrices) { // Component name Log.Debug("Start Processing " + productPrice.PricingId); ComponentData component; // Create Component in the correct folder component = client.GetDefaultData(ItemType.Component, ConfigurationManager.AppSettings["PriceFolderTcmId"]) as ComponentData; //get the correct TcmId for the Product productPrice.Product.TcmId = ProductUtilites.GetProductTcmId(client, productPrice.Product.TcmId); //set the component information component.Title = productPrice.PricingId; component.Id = "tcm:0-0-0"; //serialize the object to XML for Tridion component.Content = productPrice.Serialize(); component.Schema.IdRef = ConfigurationManager.AppSettings["PriceSchemaTcmId"]; try { //create the component client.Create(component, null); } catch (Exception ex) { Log.Error("PricingManagement.CreateComponents: Unable to save new component", ex); } Log.Debug("Finished Processing " + productPrice.PricingId + ". Count = " + count); count++; } goodMessage.Text = "Processed " + productPrices.Count + " product prices"; goodMessage.Visible = true; } catch (Exception ex) { Log.Error("PricingManagement.CreateComponents: Could not create components", ex); throw; } }
public void CreateComponents(CoreService2010Client client, List<ProductPrice> productPrices) { try { int count = 1; foreach (var productPrice in productPrices) { // Component name Log.Debug("Start Processing " + productPrice.PricingId); ComponentData component; // Create Component in the correct folder component = client.GetDefaultData(ItemType.Component, ConfigurationManager.AppSettings["PriceFolderTcmId"]) as ComponentData; //get the correct TcmId for the Product productPrice.Product.TcmId = ProductUtilites.GetProductTcmId(client, productPrice.Product.TcmId); //set the component information component.Title = productPrice.PricingId; component.Id = "tcm:0-0-0"; //serialize the object to XML for Tridion component.Content = productPrice.Serialize(); component.Schema.IdRef = ConfigurationManager.AppSettings["PriceSchemaTcmId"]; try { //create the component client.Create(component, null); } catch (Exception ex) { Log.Error("PricingManagement.CreateComponents: Unable to save new component", ex); } Log.Debug("Finished Processing " + productPrice.PricingId + ". Count = " + count); count++; } goodMessage.Text = "Processed " + productPrices.Count + " product prices"; goodMessage.Visible = true; } catch (Exception ex) { Log.Error("PricingManagement.CreateComponents: Could not create components", ex); throw; } }
public static string GetProductTcmId(CoreService2010Client client, string productId) { string productTcmId = ""; try { //get the XML list of component from the folder var productsXML = client.GetListXml(ConfigurationManager.AppSettings["ProductFolderTcmId"], new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Component } }); //loop through each item and find out if it is the product we want foreach (var product in productsXML.Elements()) { var productData = client.Read(product.Attribute("ID").Value, null) as ComponentData; var schemaFields = client.ReadSchemaFields(productData.Schema.IdRef, false, null); var content = XDocument.Parse(productData.Content); XNamespace ns = schemaFields.NamespaceUri; //check if the product id's match if (productId == content.Root.Element(ns + "product_id").Value) { //return the TcmId productTcmId = product.Attribute("ID").Value; //exit the foreach break; } } } catch (Exception ex) { Log.Error("Error in GetProductTcmId()", ex); } return(productTcmId); }
//http://blog.building-blocks.com/uploading-images-using-the-core-service-in-sdl-tridion-2011 public static void UploadImages(string location, string folderTcmId, CoreService2010Client client, log4net.ILog Log) { //create a reference to the directory of where the images are DirectoryInfo directory = new DirectoryInfo(location); //create global Tridion Read Options ReadOptions readOptions = new ReadOptions(); //use Expanded so that Tridion exposes the TcmId of the newly created component readOptions.LoadFlags = LoadFlags.Expanded; try { //loop through the files foreach (FileInfo fileInfo in directory.GetFiles()) { //only allow images if (IsAllowedFileType(fileInfo.Extension)) { try { //create a new multimedia component in the folder specified ComponentData multimediaComponent = (ComponentData)client.GetDefaultData(Tridion.ItemType.Component, folderTcmId); multimediaComponent.Title = fileInfo.Name.ToLower(); multimediaComponent.ComponentType = ComponentType.Multimedia; multimediaComponent.Schema.IdRef = ConfigurationManager.AppSettings["MultimediaSchemaId"]; //create a string to hold the temporary location of the image to use later string tempLocation = ""; //use the StreamUpload2010Client to upload the image into Tridion UploadResponse us = new UploadResponse(); using (Tridion.StreamUpload2010Client streamClient = new StreamUpload2010Client()) { FileStream objfilestream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read); tempLocation = streamClient.UploadBinaryContent(fileInfo.Name.ToLower(), objfilestream); } //creat a new binary component BinaryContentData binaryContent = new BinaryContentData(); //set this temporary upload location to the source of this binary binaryContent.UploadFromFile = tempLocation; binaryContent.Filename = fileInfo.Name.ToLower(); //get the multimedia type id binaryContent.MultimediaType = new LinkToMultimediaTypeData() { IdRef = GetMultimediaTypeId(fileInfo.Extension) }; multimediaComponent.BinaryContent = binaryContent; //save the image into a new object IdentifiableObjectData savedComponent = client.Save(multimediaComponent, readOptions); //check in using the Id of the new object client.CheckIn(savedComponent.Id, null); } catch (Exception ex) { Log.Debug("Error creating image " + fileInfo.Name, ex); } } } } catch (Exception ex) { Log.Error("Error processing images", ex); } finally { //clean up temp objects } }
//http://blog.building-blocks.com/uploading-images-using-the-core-service-in-sdl-tridion-2011 public static void UploadImages(string location, string folderTcmId, CoreService2010Client client, log4net.ILog Log) { //create a reference to the directory of where the images are DirectoryInfo directory = new DirectoryInfo(location); //create global Tridion Read Options ReadOptions readOptions = new ReadOptions(); //use Expanded so that Tridion exposes the TcmId of the newly created component readOptions.LoadFlags = LoadFlags.Expanded; try { //loop through the files foreach (FileInfo fileInfo in directory.GetFiles()) { //only allow images if (IsAllowedFileType(fileInfo.Extension)) { try { //create a new multimedia component in the folder specified ComponentData multimediaComponent = (ComponentData)client.GetDefaultData(Tridion.ItemType.Component, folderTcmId); multimediaComponent.Title = fileInfo.Name.ToLower(); multimediaComponent.ComponentType = ComponentType.Multimedia; multimediaComponent.Schema.IdRef = ConfigurationManager.AppSettings["MultimediaSchemaId"]; //create a string to hold the temporary location of the image to use later string tempLocation = ""; //use the StreamUpload2010Client to upload the image into Tridion UploadResponse us = new UploadResponse(); using (Tridion.StreamUpload2010Client streamClient = new StreamUpload2010Client()) { FileStream objfilestream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read); tempLocation = streamClient.UploadBinaryContent(fileInfo.Name.ToLower(), objfilestream); } //creat a new binary component BinaryContentData binaryContent = new BinaryContentData(); //set this temporary upload location to the source of this binary binaryContent.UploadFromFile = tempLocation; binaryContent.Filename = fileInfo.Name.ToLower(); //get the multimedia type id binaryContent.MultimediaType = new LinkToMultimediaTypeData() { IdRef = GetMultimediaTypeId(fileInfo.Extension) }; multimediaComponent.BinaryContent = binaryContent; //save the image into a new object IdentifiableObjectData savedComponent = client.Save(multimediaComponent, readOptions); //check in using the Id of the new object client.CheckIn(savedComponent.Id, null); } catch (Exception ex) { Log.Debug("Error creating image " + fileInfo.Name, ex); } } } } catch (Exception ex) { Log.Error("Error processing images", ex); } finally { //clean up temp objects } }
protected void btnUpload_Click(object sender, EventArgs e) { // Check file has been selected if (!fileUpload.HasFile || string.IsNullOrEmpty(fileUpload.PostedFile.FileName)) { badMessage.Text = "Please select a file to upload."; badMessage.Visible = true; return; } // Check file has valid extension var fileExt = Path.GetExtension(fileUpload.PostedFile.FileName); if (string.IsNullOrEmpty(fileExt) || !ValidExtensions.IsMatch(fileExt)) { badMessage.Text = "The uploaded file has an invalid name or extension."; badMessage.Visible = true; return; } try { // Save upload to disk string virtualPath = "~\\Bin\\" + fileUpload.FileName; string absPath = Server.MapPath(virtualPath); Log.Info("save file to " + absPath); fileUpload.SaveAs(absPath); // Parse XLS Log.Debug("Start XLS read"); var productPrices = ReadXlsFile(virtualPath); int count = productPrices.Count; Log.Info("End XLS read " + count); using (var client = new CoreService2010Client()) { Log.Debug("Start processing pricing components"); CreateComponents(client, productPrices); Log.Debug("End processing pricing components"); } } catch (Exception ex) { Log.Error("PricingManagement.UploadFile: Error retrieving and setting XLS", ex); badMessage.Text = "An occurred while processing the product prices." + ex.StackTrace ; badMessage.Visible = true; } }
/// <summary> /// /// </summary> public CoreServicesUtil() { this.coreServiceClient = new CoreService2010Client("basicHttp_2010"); this.readOptions = new ReadOptions(); }