sitecore programmatically change imagefield datasource - sitecore

I need image field source of a product item to change depending on the folder path I am creating a product item under when creating the item in content or page editor.
If I am creating a product under /home/bicycles, I need the product item image field to auto change to /sitecore/media library/Images/bicycles
If I am creating a product under /home/cars, I need the product item image field to auto change to /sitecore/media library/Images/cars
If I am creating a product under /home/scooters, I need the product item image field to auto change to /sitecore/media library/Images/scooters
The default setting for that image field source in the datatemplate is /sitecore/media library/Images/bicycles
How can I go about doing this?

One option is to create a custom Content Editor field that extends the default image field type.
Start by creating a class that inherits from Sitecore.Shell.Applications.ContentEditor.Image. Then override the OnPreRender method to determine and set the Source property of the image field based on your location criteria/requirements.
See comments in the code below for more information.
public class ContextAwareImageField : Sitecore.Shell.Applications.ContentEditor.Image
{
/// <summary>
/// The ItemID proprety is set by the Content Editor via reflection
/// </summary>
public string ItemID { get; set; }
/// <summary>
/// Override the OnPreRender method.
/// The base OnPreRender method assigns a value to the Source viewstate property and we need to overwrite it.
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Source = GetSource();
}
protected virtual string GetSource()
{
//retrieve and return the computed source value if it has already been set
var contextSource = GetViewStateString("ContextSource");
if (!string.IsNullOrWhiteSpace(contextSource))
return contextSource;
//retrieve the context item (the item containing the image field)
var contextItem = Sitecore.Context.ContentDatabase.GetItem(ItemID);
if (contextItem == null)
return string.Empty;
//determine the source to be used by the media browser
//in this case we're just checking based on parent path, but any logic could be inserted
contextSource = "/sitecore/media library/Images";
switch (contextItem.Parent.Paths.FullPath.ToLowerInvariant())
{
case "/sitecore/content/home/bicycles":
contextSource = "/sitecore/media library/Images/Bicycles";
break;
case "/sitecore/content/home/cars":
contextSource = "/sitecore/media library/Images/Cars";
break;
case "/sitecore/content/home/scooters":
contextSource = "/sitecore/media library/Images/Scooters";
break;
}
//store the computed source value in view bag for later retrieval
SetViewStateString("ContextSource", contextSource);
//return the computed source value
return contextSource;
}
}
Next, perform the following steps:
Login to the Sitecore desktop with administrator rights and use the database icon in the lower right to switch to the Core database.
In the Core database, open the Content Editor then navigate to /sitecore/system/Field Types/Simple Types. There you will find an item representing the Image field type.
Duplicate the Image field type item and rename the duplicated item to something relevant (e.g. Context Aware Image).
Edit the duplicated item
In the Assembly field, provide the name of the assembly file containing your custom image field class (e.g. MyClient.MySite.dll)
In the Class field, provide the name of the custom image field class, including the namespace (e.g. MyClient.MySite.CustomFields.ContextAwareImageField)
Delete the value in the Control field
Save your changes
Switch back to the Master database.
Open the Content Editor, then navigate to a template that should contain your new image field.
Either create a new field in the template and choose your new custom field type in the Type dropdown. Or, change the Type for an existing image field.
Save your template changes.
In the content tree, navigate to an item based on the template above and click the Browse button for the contained image field. The media browser dialog should default to the source location specified by the logic in your custom field.
Note: if you are using a version of Sitecore that contains a SPEAK-based media browser dialog, you will have to switch to Tree View in the dialog (icon in the upper right) in order to see the source location specified by your custom field.

Related

Acumatica-LotSerialNbrAttribute screen on mobile app

I'm using Acumatica customization Acumatica-LotSerialNbrAttribute
This customization adds a new screen for look for InventoryID and LotSerialNbr and visualize its attributes.
I'm trying to add this window to the acumatica mobile app.
Here my code:
add screen IN202501 {
add container "InventoryLotSerialContainers" {
add field "InventoryID"
add field "LotSerialNbr"
add group "Attributes" {
displayName = "Attributes"
collapsable = True
add attributes "AttributesAttributes"
}
add recordAction "Save" {
behavior = Save
}
add recordAction "Cancel" {
behavior = Cancel
}
attachments {
}
}
}
And the screen is visible on the mobile app with the 2 selectors
Then I select Inventory and when I select Lot Serial Nbr, the first selector is in blank, causing that I can't review the attributtes neither save the information.
Here the InventoryID selector in blank.
Hope you can help me to successfully publish this screen on acumatica mobile app.
Thanks.
Attributes are a grid style representation in Acumatica. That means you need a container that will show multiple records. I'm getting rusty on mobile pretty quickly, but I believe your definition is set to display a single value value only.
Try adding a separate container for attributes:
add container "AttributesAttributes" {
add field "Attribute"
add field "Value"
}
This should open into a view of multiple records, showing all of your attributes. By specifying the Attribute and Value fields, you should see both data elements in the container.

Replacing Field values in Word Document QAxObject QT / C++

I'm really new to QT and I was tasked to update some field values in word document programmatically, currently I can replace text from word document fine, but when that field value is inside an object (table or anything) it's not working, my code is:
QString outFile("D:\\#test files\\output.docx");
QString inFile1("D:\\#test files\\input.docx");
QAxObject axObject("Word.Application");
QAxObject* documents = axObject.querySubObject("Documents");
QAxObject* document = documents->querySubObject("Open(const QString&, bool)", inFile1, true);
QAxObject* selection = axObject.querySubObject("Selection");
auto find = selection->querySubObject("Find");
QString sOld = "${name}";
QString sNew = "Ibrahim";
bool bMatchCase = false;
bool bMatchWholeWord = false;
bool bMatchWildCards = false;
bool bReplaceAll = true;
QVariantList vl = { sOld, bMatchCase, bMatchWholeWord, bMatchWildCards, false, false, true, 1, false, sNew, bReplaceAll ? "2" : "1" };
find->dynamicCall("Execute(QString,bool,bool,bool,bool,bool,bool,int,bool,QString,int)", vl);
document->dynamicCall("SaveAs(const QString&)", outFile);
document->dynamicCall("Close()");
axObject.dynamicCall("Quit()");
If you can help it would be awesome :)
If you can change the nature of the target files, you would do well replacing your targets with real Word DocVariable or DocProperty fields. Then use your code to change the variable or properties and update the related fields in the documents. Some document properties (under the Quick Parts > Document Properties menu) are mapped to XML data points and do not require updating of fields if those are used.
The placeholder can be (1) a DocVariable field or (2) a DocProperty field. You can change the variables or properties using code and then update the field.
You can also use one of the built-in mapped Document Property Content Controls in which case there is no need to update a field if the property is changed. It is automatic. More on these in my related page: Repeating Data Mapped Document Property Content Controls or Other Mapped Content Controls.
Here are links to two Word MVP pages on accessing Document Properties using vba.
How Can I Get Access to the Document Properties of a Document Without Opening the Document
How to Use a Single vba Procedure to Read or Write Both Built-In and Custom Document Properties

Sitecore custom ribbon button not working

I have created a custom ribbon button following the steps mentioned in http://jondjones.com/how-to-add-a-custom-sitecore-button-to-the-editor-ribbon/
I can see the button appearing in sitecore:
Custom button
Command does not get triggered when clicked on the button.
Below is my code:
using System;
using Sitecore.Shell.Applications.Dialogs.ProgressBoxes;
using Sitecore.Shell.Framework.Commands;
namespace SitecoreVsPoc.Commands
{
public class TranslateContent : Command
{
private static readonly object Monitor = new object();
public override void Execute(CommandContext context)
{
if (context == null)
return;
try
{
ProgressBox.Execute("Arjun", "Title", "Applications/32x32/refresh.png", Refresh);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error!", ex, this);
}
}
public void Refresh(params object[] parameters)
{
// Do Stuff
}
}
}
Below is the command I have registered in commands.config:
<command name="contenteditor:translatecontent" type="SitecoreVsPoc.Commands.TranslateContent,SitecoreVsPoc" />
Note: I am using Sitecore 8.2 initial release.
Can someone suggest a solution for this?
In Sitecore 8 it was changed the way you add Ribbon button. As far I see your link is from Sitecore 7 or 6.
To create the new button item for the Experience Editor ribbon:
In the Core database, open the Content Editor and navigate to /sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Page Editor/Edit.
Create a new item based on the relevant ribbon control template, for example, the Small Button template. The templates are located at /sitecore/templates/System/Ribbon/.
For the new item, add the following information:
In the Header field, enter the display name of the button.
In the ID field, enter a unique identifier for the item. For example, you can include the ribbon group name in the ID.
In the Icon field, enter the path to the relevant icon. Depending on the button you create, adjust the icon size accordingly.
Open Sitecore Rocks and add the relevant control rendering, for example SmallButton, to the layout of the button item you created.
Enter a unique ID for the rendering.
For other SPEAK controls, you can point to another item in the Data Source field and specify the configuration in this other item.
Important
More informations you can find here: https://doc.sitecore.net/sitecore_experience_platform/content_authoring/the_editing_tools/the_experience_editor/customize_the_experience_editor_ribbon
http://reyrahadian.com/2015/04/15/sitecore-8-adding-edit-meta-data-button-in-experience-editor/
Before it was very simple, you didn't need to add new code:
https://blog.istern.dk/2012/05/21/running-sitecore-field-editor-from-a-command/

Sitecore: multilist during deployment

How to enable the multilist to be control in content editor?
for example I have a list of item, item1 to item10. In the standard template value, I defined item1,2,3. After I have deploy the solution, how am I going to enable users in content editor mode or page editor mode to select item7,8,9 and 10?
And also, after I tested/rendered the multilist, only RAW VALUES are being rendered, is there any possible to render the item name such as item1? Do I need to customize the multilist?
The multilist control should be directly visible to the user in the Content Editor, you do not need to do anything else. Since you defined some items in standard values then those will be "pre-selected" when that item is first created. The user can then add the additional items as required.
To allow users to select values from the Page Editor you can Use Sitecore EditFrame in PageEdit
The reason the item is being rendered as the raw value is because you need to get the item and then iterate over the target id's. There is an example of this here here
//Get a multilist field from the current item
Sitecore.Data.Fields.MultilistField multilistField = Sitecore.Context.Item.Fields["myMultilistField"];
if (multilistField != null)
{
//Iterate over all the selected items by using the property TargetIDs
foreach (ID id in multilistField.TargetIDs)
{
Item targetItem = Sitecore.Context.Database.Items[id];
litItemTitle = targetItem.DisplayName;
// Do something with the target items
// ...
}
}
You can use the following instead for the datasource of a repeater
Sitecore.Data.Fields.MultilistField multilistField = Sitecore.Context.Item.Fields["myMultilistField"];
Sitecore.Data.Items.Item[] items = multilistField.GetItems();

How to render referenced items from current item in Sitecore?

Let say I have item which has its presentation details configured.
In that item I have TreelistEx field keeping reference (GUIDs) to 10+ other items (different templates) somewhere in the tree structure each of them has their own presentation details (sublayouts).
How can I present all reference items on the same page ( as current item) based on their own settings?
I would like to see one page and 10+ pieces of content each with its own layout presentation.
I had a similar problem. Here is the code for rendering sublayouts and xsl renderings:
public IEnumerable<Control> GetRenderingControls(Item item)
{
// Loop through all renderings on the item
foreach (RenderingReference rendering in item.Visualization.GetRenderings(Context.Device, false))
{
// Get the path to the Sublayout
string path = rendering.RenderingItem.InnerItem["Path"];
switch(rendering.RenderingItem.InnerItem.TemplateName.ToLower())
{
case "xsl rendering":
// Create an instance of a XSL
XslFile xslFile = new XslFile();
xslFile.Path = path;
xslFile.DataSource = item.Paths.FullPath;
xslFile.Parameters = GetParameters(xslFile);
yield return xslFile;
break;
case "sublayout":
// Create an instance of a sublayout
Sublayout sublayout = new Sublayout();
sublayout.Path = path;
sublayout.DataSource = item.Paths.FullPath;
sublayout.Parameters = GetParameters(sublayout);
yield return sublayout.GetUserControl();
break;
default:
throw new Exception(string.Format("Unknown rendering template - {0}", rendering.RenderingItem.InnerItem.TemplateName));
}
}
}
Once you get the Controls you can add them to a placeholder and they will be rendered to the page.
Each item with its own layout presentation implies you are getting an entire HTML file... each item will have its own <html>, <head>, and <form> tags. Needless to say, that's going to create a problem.
That being said... This is exactly what Server.Execute() is for. Loop through your TreeList items, use LinkManager to get the URL for the item, and then Server.Execute() to get the rendered output.