Disable source parameter on links - sharepoint

Brief description of the default behavior i want to override:
Creating a SharePoint list and using the "Title (linked to Item)" in a view provides a link to the DispForm for that list. By default, this link appends a Source parameter to the URL query string.
I would like to disable this behavior in some way while still retaining the default list view web part that is created by SharePoint when the view is defined.
To be clear, I know how to convert the list-view webpart to XSLT and then control this behavior. If at all possible, I'd like to keep from doing that so that the list view can still be easily supported by Help Desk and also keep the benefits of the standard list view (such as exporting to excel and so forth).
Any ideas?
Thanks in advance for your help.
Troy

SharePoint (2007) dynamically appends the source parameter in a javascript method named GoToLink. That method is defined in SharePoint's core.js file, and it can be easily customized without editing the original file. In fact, you never want to edit that file directly; it will put SharePoint into an "unsupported" state.
We'll create a solution for our customization, so it can easily be deployed or retracted in a consistent manner. Create a new Empty SharePoint solution in Visual Studio, targeted for GAC deployment. Within your project, create the following folder structure:
Templates
Layouts
1033
("1033" is for the English localization; if you're using a different language, your id will be different).
Within "1033", create a new Javascript file (we call our ours "CustomCore.js"). This will contain JUST the code we want to override from Core.js. Since GoToLink is the only thing we're interested in, let's focus on that. Here's the original from Core.js:
function GoToLink(elm) {
if (elm.href==null)
return;
var ch=elm.href.indexOf("?") >=0 ? "&" : "?";
var srcUrl=GetSource();
if (srcUrl !=null && srcUrl !="")
srcUrl=ch+"Source="+srcUrl;
var targetUrl=elm.href+srcUrl;
if (isPortalTemplatePage(targetUrl))
window.top.location=STSPageUrlValidation(targetUrl);
else
window.location=STSPageUrlValidation(targetUrl);
}
Not much to it. For our override, we just need to omit the bits that add the source parameter. From my reckoning, the modified method looks like this:
function GoToLink(elm) {
if (elm.href==null)
return;
if (isPortalTemplatePage(elm.href))
window.top.location=STSPageUrlValidation(elm.href);
else
window.location=STSPageUrlValidation(elm.href);
}
That should be it. Package up the solution and Visual Studio should interpret the "Templates" folder structure correctly when it builds the WSP (look in the generated manifest file for the TemplateFile element and that it's deploying to "Layouts\1033\CustomCore.js").
Once your WSP solution is deployed to SharePoint, we have the final step of referencing it in your Master page. In the HEAD section of your master page, you should see Core.js being referenced like this:
<SharePoint:ScriptLink Language="javascript" runat="server" Defer="True" Name="core.js"/>
We simply add a reference to the new JS file directly beneath this line:
<SharePoint:ScriptLink Language="javascript" runat="server" Defer="True" Name="core.js"/>
<SharePoint:ScriptLink Language="javascript" runat="server" Defer="True" Name="customcore.js"/>
Test it out and make sure it works. Note that this will affect ALL lists on the server to which you've deployed "CustomCore.js" and which use the master page.

Related

Page template - Object reference not set to an instance of an object

I have a page template that has this line of code below:
<div id="<%= CurrentDocument.NodeAlias %>" class="lvl3">
Things work - the id value shows correctly. However, when using the Page Templates module Design tab, there's an error. In Event log, I saw the error message "Object reference not set to an instance of an object". Taking the id snippet out, the error is gone. I'm not sure what the error means and how to fix it. Could you help?
I do need some page-specific unique identifier in there (NodeAlias in this case) for other purposes.
The answer is pretty simple - if you open your page template in Kentico Page Templates application - your CurrentDocument is always null (because you are editing Template itself without specific document).
You could do absolutely the same thing in Pages application with the Design tab. It's also possible to edit all the template properties from there, including the Layout:
Using code blocks on page template layout is not supported. I assume you are using ASCX layout. Moreover your code looks more like a macro - so, maybe you are using HTML layout type? If yes, then the syntax should be {%CurrentDocument.NodeAlias%}
If you are using ASCX layout you may face also this issue: https://devnet.kentico.com/articles/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks
Note that in portal engine the life cycle is bit modified, so we cannot ensure the code blocks will work every time - it may work sometimes. It mainly depends on what other web parts/controls and in what order are placed in the page template. I would rather recommend using the workaround from the article.
Or, you can give it a try, with no guarantee, this may work in certain cases, you need to specify the full namespace:
<%= CMS.MacroEngine.MacroResolver.Resolve("{%CurrentDocument.NodeAlias#%}") %>

Include javascript file in sharepoint visual web part

I am using sharepoint 2010 and developing a visual web part. I had javascript functions on the ascx file and they all used to work file.Now I have moved them to a single javascript.js file and deployed it to the _layouts folder on server.
And I have put the below line on the ascx file to reference to that file
<script src="/sites/xxxxx/_layouts/customwebparts/javascript.js" type="text/javascript"> </script>
and then on the ascx.cs file and I am using the methods defined in this file for eg.,
btnCancel.Attributes.Add("onclick", "{return Action(Cancel the form?)};");
But it is not working, it is not displaying the confimration box ...Am I referencing the wrong way. Please help me ....
To reference a javascript file from within a SharePoint Visual Web Part you need to use the SharePoint:ScriptLink tag (example below):
<SharePoint:ScriptLink ID="<someid>" runat="server" Name="/_layouts/...<Path>"></SharePoint:ScriptLink>
I add a layouts mapped folder to my Visual Web Part project and place a scripts folder within the default folder, which I believe is the projects name. Following this model ensures that the javascript file is always kept up-to-date during deployment. The 'Name' property in the above tag is just the path to the javascript file relative to _layouts, so something like "/_layouts/ProjectName/Scripts/myjavascript.js".
As an FYI if you want to use CSS as well there is a SharePoint:CssRegistration tag as well. I find these are the 2 that I use the most.
You should put your javascript file in the Layout mapped folder and use the SharePoint:ScriptLink tag:
<SharePoint:ScriptLink id="ScriptLink1" runat="server" Localizable="false" Name="some-layout-subfolder/file.js" />
If you have a file not found error you should check that the Localizable attribute is set to false and from Visual Studio, in the properties of the js file, Build Action should be set as Content and Deployment Type should be TemplateFile.
Check here for more details:
http://blog.netgloo.com/2014/06/19/include-javascript-and-css-files-in-your-sharepoint-2010-visual-web-part/

asp.net webpages content block and helper differences

In asp.net webpages framework what is the difference between using a content block versus a helper?
They both seem to be used to output HTML to multiple pages. They both can contain code and both can pass parameters.
Are there other differences? When should you use a helper versus a content block?
More info:
With Content Blocks we create a .cshtml (for example _MakeNote.cshtml) file to hold the content we want to insert into a page. Then we use:
#RenderPage("/Shared/_MakeNote.cshtml")
to insert the content into a page. We can pass parameters to the content block like this:
#RenderPage("/Shared/_MakeNote.cshtml", new { content = "hello from content block" })
It's somewhat like an include file, but I think does not share scope with the parent page.
With Helpers we create a .cshtml page in the App_Code folder (for example MyHelpers.cshtml) and place methods in that page which we want to call. The method looks something like this:
#helper MakeNote(string content) {
<div>#content</div>
}
The helper is called by using:
#MyHelpers.MakeNote("Hello from helper")
There isn't a lot of difference functionally. Helpers need to go into an App_Code folder - unless you download VWD or Visual C# Express and compile a binary - and the App_Code folder doesn't translate well to the MVC framework. Of course, that's only relevant if you want to upgrade to MVC at some point.
I would use a helper for generic functional snippets like your MakeNote. I would use a "content-block" (partial, really) for repeated site-specific sections of a page.

How to add extra css to SharePoint 2007 site without reverting back to default theme

Currently I'm having trouble adding custom/my own css files to my SharePoint site. I add the custom/my own css files via the c# files (CssLink) as a web part and have them applied to my site's javascript files. Trouble is, whenever the css files were applied, my site goes back to the default blue-ish SharePoint theme color.
Is there any quick and simple way to avoid that from happening?
Thanks.
You can use this method to register a css file from a WebPart
Microsoft.SharePoint.WebControls.CssRegistration.Register("/.../mystyles.css")
or you can add the css file to the content place holder with the id "PlaceHolderAdditionalPageHead" which is present in the master page like this
var placeholder= Page.FindControl("PlaceHolderAdditionalPageHead");
var cssLink = new Literal();
cssLink.Text = "text";
placeholder.Controls.Add(cssLink);
Why don't you use, Site Settings, Master page and use the option "Specify a CSS file to be used by this publishing site and all sites that inherit from it." to specify your own CSS.
(Also, I have no idea what you mean with "via the c# files (CssLink) as a web part and have them applied to my site's javascript files". Are you missing some words in that sentence?)
You should have a very good reason to be adding CSSLink via C#. Have you considered packaging your CSS as your own theme?
SharePoint themes are easy to create and have many benefits such as:
a) Supported by Microsoft
b) Easy to create
c) Manageable by the end users.
d) You can apply different themes to different parts of the site.
e) etc etc...
The process of creating the theme can be found here:
http://sharepoint.microsoft.com/blogs/GetThePoint/Lists/Posts/Post.aspx?ID=122
I would recommend adding your theme via a feature only for adding and removing the theme. This would add a great deal of options for future tweaking.
Here is an example:
http://www.devexpertise.com/2009/02/11/installing-a-theme-as-a-sharepoint-feature/
I'm going to blog about this later this week so keep an eye out of you like. http://blog.zebsadiq.com
Upload your css in the syle library folder(or any library in side your site)
go to -->site actions-->site settings-->modify all site settings-->under look and feel tab-->click master page-->there is one option called alternate css url-->browse your custom css and click Ok.

Change Edit Control Block (ECB) Link URL in SharePoint

Is there a way to dynamically change the hyperlink associated with an ECB menu in WSS 3.0? For instance, I have a list with 2 fields. One field is hidden and is a link, the other is the title field which has the ECB menu. The title field currently links to the item's view page - but we want it to link to the link-field's url. Is that possible?
UPDATE - 5/29/09 9AM
I have this so far. See this TechNet post.
<script type="text/javascript">
var url = 'GoTo.aspx?ListTitle='+ctx.ListTitle;
url += '&ListName='+ctx.listName;
url += '&ListTemplate='+ctx.listTemplate;
url += '&listBaseType='+ctx.listBaseType;
url += '&view='+ctx.view;
url += '&';
var a = document.getElementsByTagName('a');
for(i=0;i<=a.length -1;i++)
{
a[i].href=a[i].href.replace('DispForm.aspx?',url);
}
</script>
This gives me a link like so (formatted so it's easier to see):
GoTo.aspx
?ListTitle=MyList
&ListName={082BB11C-1941-4906-AAE9-5F2EBFBF052B}
&ListTemplate=100
&listBaseType=0
&view={9ABE2B07-2B47-4390-9969-258F00E0812C}
&ID=1
My issue now is that the row in the grid gives each item the ID property above but if I change the view or do any filtering you can see that the ID is really just the row number. Can I get the actual item's GUID here?
If I can get the item's ID I can send it with the list ID to an application page that will get the right URL from field in the list and forward the user on to the right site.
I think the easiest solution and one I use regularly to modify default sharepoint functionality without having to install server side code is to inject some javascript onto the page to make the necessary modifications.
The Content Editor webpart is ideal for this if you don't want to edit the page source itself. Together with the IE Developer Toolbar or Firebug to inspect the elements you want to edit you should be able to achieve what you need with just a couple of lines of javascript.
Let me know if you need any further detail on getting this work.
The title/link to edit menu is a computed field - basically a combination of the title and item id. If you look at the definition of the field (off the top of my head I think it's in fields.xml) you should be able to create a modified version in your schema.xml that uses the url field in its RenderPattern.
Following up on Tom's answer, you can use the SharePoint Solution Generator in VseWss 1.3 to generate a Visual Studio solution that can re-create your list. You will faint when you see the huge amount of XML that the views use in the schema.xml file but you will see the render pattern that Tom referred to and you should be able to get a general idea of how to modify it to suit your needs.
Gotta love SharePoint. Where small customizations means "take what I give you or rewrite it from scratch"

Resources