How do you add custom menu actions programmatically in SharePoint? - sharepoint

I need to add a custom menu action to a custom content type programmatically in c#. This is because I will not know the URL I need to link to beforehand. The URL to link to will be pulled from configuration when the feature is activated.
I have tried the following:
Added the CustomAction in my Element.xml file as:
<CustomAction
Id="MyID"
RegistrationType="ContentType"
RegistrationId="0x010100ef19b15f43e64355b39431399657766e"
Location="EditControlBlock"
Sequence="1000"
Title="My Menu Item">
<UrlAction Url="" />
</CustomAction>
In my feature receiver FeatureActivated method, I have:
SPElementDefinitionCollection eleCollection =
properties.Feature.Definition.GetElementDefinitions(
new System.Globalization.CultureInfo(1));
foreach (SPElementDefinition ele in eleCollection)
{
if (ele.Id == "MyID")
{
System.Xml.XmlNode node = ele.XmlDefinition.FirstChild;
node.Attributes[0].Value = "MY URL";
ele.FeatureDefinition.Update(true);
}
}
I would expect this code to update the UrlAction Url with "MY URL" but it does not. If I hard code a URL in the XML it works but I must be able to do it programmatically.

You can use the SPUserCustomActionCollection on the SPWeb object:
using (SPSite site = new SPSite("http://moss.dev.com"))
using (SPWeb web = site.OpenWeb())
{
SPContentType contentType = web.ContentTypes["Curriculum Vitae"];
SPUserCustomAction action = web.UserCustomActions.Add();
action.RegistrationType = SPUserCustomActionRegistrationType.ContentType;
action.RegistrationId = contentType.Id.ToString();
action.Location = "EditControlBlock";
action.Sequence = 450;
action.Title = "Test";
action.Rights = SPBasePermissions.EditListItems;
action.Url = "http://www.google.com";
action.Update();
}
This way, you can set the URL to whatever you want. If you are updating an existing custom action, you can iterate through the collection and update the one you are looking for. Updating the element XML definition after you've installed the custom action doesn't do anything.

Depending on what you want to achieve, you can use some javascript;
<UrlAction Url="JavaScript:window.location='{SiteUrl}/_layouts/CustomListAction.aspx?ID={ListId}'"/>
the ~site and ~siteCollection also works:
<UrlAction Url="~site/_layouts/Page.aspx?ID={ListId}"/>

I don't think the WSS schema definition allows for an empty Url attribute in the UrlAction element. Maybe try putting a "default" url in the xml that you overwrite later?

Related

Highlight links in a Quick Links web part on SharePoint modern page

We have added 4 "Quick Links" web/app part in one section on a SharePoint modern page. We would like to highlight links under "Quick Links web part 2" and "Quick Links web part 4" only. I have added React modern script editor. How do we archive the above requirement using CSS ? If it is not possible in CSS then we would like to introduce JS. I couldn't find a fixed tag name that I can grab and apply CSS except GUID.
You could try to inject CSS by SPFX.
Check the demo shared by hugoabernier.
Main code:
export default class InjectCssApplicationCustomizer
extends BaseApplicationCustomizer<IInjectCssApplicationCustomizerProperties> {
#override
public onInit(): Promise<void> {
Log.info(LOG_SOURCE, `Initialized ${strings.Title}`);
const cssUrl: string = this.properties.cssurl;
if (cssUrl) {
// inject the style sheet
const head: any = document.getElementsByTagName("head")[0] || document.documentElement;
let customStyle: HTMLLinkElement = document.createElement("link");
customStyle.href = cssUrl;
customStyle.rel = "stylesheet";
customStyle.type = "text/css";
head.insertAdjacentElement("beforeEnd", customStyle);
}
return Promise.resolve();
}
}

Accessing HTML controls inside iFrame

How do you access html controls inside an iframe from javascript in CRM?
I have:
var height = document.getElementById("IFRAME_TransactionProduct_RA").contentWindow.document.getElementById("txt").value;
but that results in "Error on page" and the content is not loaded.
The element I want to access is an html input with id of 'txt':
<input id="txt" type="hidden" />
Here's an example how you copy a value from a CRM field to a control in an embedded HTML control in an IFRAME. I'm assuming the names of the web resource and the field. You'll have to adapt those. You also might throw in a try-catch in case CRM throws in en exception (got the joke?) and please mind that I'm typing the code on my phone so there might be a typo somewhere (auto-correction, yey).
var source = Xrm.Page.data.entity.attributes.get("oneCoolField")
var information = source.getValue();
var customHtml = Xrm.Page.ui.controls.get("WebResource_EmbeddedHtmlContent");
var destination = customHtml.getObject().contentWindow.document;
if(destination) {
var customControl = destination.getElementById("elementToAccess");
if(customControl) {
customControl.value = information;
}
}
EDIT:
This gets you to the web resource.
var customHtml = Xrm.Page.ui.controls.get("WebResource_EmbeddedHtmlContent");
This gets you to the DOM of the IFRAME.
var destination = customHtml.getObject().contentWindow.document;
This gets you to the control on the custom page.
var customControl = destination.getElementById("elementToAccess");
This gets you the contents of the control.
var contents = customControl.innerHTML;
Which part fails on your computer?
With jQuery:
$(Xrm.Page.ui.controls.get('IFRAME_TransactionProduct_RA').getObject()).contents().find('#txt').val();
Pure JS:
Xrm.Page.ui.controls.get('IFRAME_TransactionProduct_RA').getObject().contentWindow.document.getElementById('txt').value;
http://msdn.microsoft.com/en-us/library/gg334266.aspx#BKMK_getObject

How to extend Orchard navigation module to add images to menu items

UPDATE: I've changed the original question drastically based on Bertrand's suggestions and my own findings. Now it provides an incomplete solution in its text instead of my own blind meanderings and commentary on Orchard, which were completely WRONG!
I need to display a menu using images instead of text, one standard, and another for when hovered/selected. The requirements for the site states that the end-user should be able to manage the menu item images. The standard navigation module now provides an HTML menu item, which is not what the end user wants. The customer wants a very simple, intuitive interface for configuring the sites many menus, and all menus are image-based.
Based on Bertrand's advice, and after realizing that Content Menu Item IS A CONTENT TYPE, I've created a new Content Part in the Admin Interface (not by code, I only want to write code for parts and content types when ultimately needed... I really want to see how far I can go with Orchard just by using the admin interface and templating/CSSing).
So, I've created a Menu Image Part, with two Content Picker fields added to it: Image and Hover Image. Then I've added this part to the Content Menu Item in the Manage Content Items admin interface.
Since I didn't write a Driver for it, the Model passed to the menu item template does not have an easily accessible property like #Model.Href... I've overriden the MenuItemLink-ContentMenuItem.cshtml with the following code so far:
#using Orchard.Core.Common.Models
#using Orchard.ContentManagement
#{
var contentManager = WorkContext.Resolve<IContentManager>();
var itemId = Model.Content.ContentItem.ContentMenuItemPart.Id;
ContentItem contentItem = contentManager.Get(itemId);
ContentField temp = null;
var menuImagePart = contentItem.Parts.FirstOrDefault(p => p.PartDefinition.Name == "MenuImagePart");
if (menuImagePart != null)
{
temp = menuImagePart.Fields.First();
}
}
<span>#temp</span>
#Model.Text
This yields the expected title for the Menu in a link, with a span before it with the following text:
Orchard.Fields.Fields.MediaPickerField
So all the above code (get the current content manager and the id of the ContentItem representing the ContentMenuItemPart, then use the content manager to get ContentItem itself, then linqing over its Parts to find the MenuImagePart (I can't use Get to get it because it requires a type and the MenuImagePart is not a type, it was created in the admin interface), then finally getting the first field for debugging purposes (this should be the Image field of the MenuImagePart I've created...)... all the above code actually got me to the Media Picker Field on my Meny Image Part...
What I'm not being able to do, and what makes me certainly a lot obtuse and stupid, is to find a way to read the MediaPickerField URL property! I've tried casting it to MediaPickerField, but I can't access its namespace from inside my template code above. I don't even know which reference to add to my theme to be able to add the following directive to it:
#using Orchard.Fields.Fields
I've finally succeeded in this task (thanks to Bertrand's direction).
UPDATE: And thanks again to Bertrand I've polished the solution which was running in circles, querying content items from the content manager when they were already available on the Model... now I'm leveraging the dynamic nature of content item, etc. And I'm finally satisfied with this solution.
It was necessary to create a new Content Part called Menu Image, then add this to the Content Type named Content Item Menu, and finally overriding the Content Item Menu template. This last part was the really tricky one. If it was not for Bertrand's directions the code bellow would have been smelly and daunting. The template ended up as follow:
#using Orchard.Utility.Extensions;
#using System.Dynamic
#{
/* Getting the menu content item
***************************************************************/
var menu = Model.Content.ContentItem;
/* Creating a unique CSS class name based on the menu item
***************************************************************/
// !!! for some reason the following code throws: 'string' does not contain a definition for 'HtmlClassify'
//string test = menu.ContentType.HtmlClassify();
string cssPrefix = Orchard.Utility.Extensions.StringExtensions.HtmlClassify(menu.ContentType);
var uniqueCSSClassName = cssPrefix + '-' + Model.Menu.MenuName;
/* Adds the normal and hovered styles to the html if any
***************************************************************/
if (menu.MenuImagePart != null)
{
if (!string.IsNullOrWhiteSpace(menu.MenuImagePart.Image.Url))
{
using(Script.Head()){
<style>
.#uniqueCSSClassName {
background-image: url('#Href(menu.MenuImagePart.Image.Url)');
width: #{#menu.MenuImagePart.Image.Width}px;
height: #{#menu.MenuImagePart.Image.Height}px;
display: block;
}
</style>
}
}
if (!string.IsNullOrWhiteSpace(menu.MenuImagePart.HoverImage.Url))
{
using(Script.Head()){
<style>
.#uniqueCSSClassName:hover {
background-image: url('#Href(menu.MenuImagePart.HoverImage.Url)');
width: #{#menu.MenuImagePart.HoverImage.Width}px;
height: #{#menu.MenuImagePart.HoverImage.Height}px;
}
</style>
}
}
}
}
<a class="#uniqueCSSClassName" href="#Model.Href">#Model.Text</a>
The only thing that I didn't understand is why I can't use HtmlClassify as an extension method with menu.ContentItem.HtmlClassify() and have to resort to calling the method as a standard static method (see the line with the comment `// !!! for some reason the following code throws´...)
Thanks again Bertrand!

Programmatically Set the Title of a SharePoint Page?

I need to set the page title (Page Title) of a SharePoint page in code. I have already tested
this.Page.Title = "My Page Title";
But this does not change the title when the page loads. Can anyone offer any advise on how to do this?
Thanks, MagicAndi
This blog post by Michael Becker gives a method of modifying the SharePoint Page title using the code below:
ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) Page.Master.FindControl("PlaceHolderPageTitle");
contentPlaceHolder.Controls.Clear();
LiteralControl literalControl = new LiteralControl();
literalControl.Text = "My Page Title";
contentPlaceHolder.Controls.Add(literalControl);
If you want to change the page title from a webpart on the page for example, you could use this:
private void ChangeTitle(string newTitle)
{
SPListItem item = SPContext.Current.ListItem;
if (item != null)
{
item[SPBuiltInFieldId.Title] = newTitle;
item.SystemUpdate(false);
}
}
This will only work for a page in the pages library, because the default.aspx page in the root of your site doesn't have an associated listitem. Also don't forget to refresh your page after changing the title.
The SystemUpdate makes sure that 'modified/modified by' information is not updated and that the version number doesn't increase. If you want this information updated, replace it by item.Update();

Dynamically setting the ListId in the <SharePoint:ListView> control

Is there a way to dynamically set the ListId field in the ListView control. We cannot guarantee that the list we are interested in has a consistent GUID between installations (The list is deployed as part of a site template that we do not control). I've tried using the PreInit event to set a variable (see the list guid: section. If I remove the ListView tag, I see the proper GUID printed out. So I'm collecting the GUID correctly. However, The listview control errors out with the following message "Guid should contain 32 digits with 4 dashes". This tells me that the tag is not getting the variable set. Is this correct? Is there another way to specify the list to use?
Can this be done?
Sample code follows:
<%# Register TagPrefix="Sharepoint" ...details deleted.. %>
<br>
...stuff deleted.
<br>
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
... more stuff deleted...
<p>list guid: <%=ListGuid %>
<Sharepoint:ListView ListId="<%=ListGuid %>" Enabled="true" runat="server" />
<p>
</asp:Content>
<script runat="server">
string ListGuid = string.Empty;
protected void Page_PreInit(object sender, EventArgs e)
{
SPSite Site = SPContext.Current.Site;
using (SPWeb HelpDesk = Site.OpenWeb("HelpDesk"))
{
SPList list = HelpDesk.Lists["Charge Numbers"];
ListGuid = list.ID.ToString();
}
}
</script>
Having server-side code like you do that gets injected into more server side code is a little weird and seems unlikely to work. What does the SharePoint log say about the list ID that it's trying to load? My guess is that it's not really trying to load the list represented by the GUID that you're setting in your ListGuid variable.
Instead of trying to force some global ListGuid variable to have the correct guid, why not just set the control's ListId property inside your application page's Load event handler?
In the aspx file:
<SharePoint:ListView Id="lv" runat="server" />
In the application page's codebehind:
protected ListView lv
In the Load event:
SPList list = HelpDesk.Lists["Charge Numbers"];
Guid myguid = list.ID;
lv.ListId = myguid.ToString();
I think the error message you are receiving is telling you what's going wrong... the ToString() is converting the GUID to an invalid format. There is an overload for the tostring
list.ID.ToString("N");
list.ID.ToString("D"); (this is the default)
list.ID.ToString("B");
list.ID.ToString("P");
... Try them all

Resources