Composite C1: How to make the page tree to display UrlTitle instead of Title (Solution) - c1-cms

When Composite C1 displays the tree of sites and pages in the Content perspective, it uses the page Title property as the node label. This is not always comfortable, especially when the pages are translated to different cultures, so you can't even read those titles and quickly find the page you are looking for.
The idea is to use the UrlTitle as the label, so every tree node would represent a part of the page URL.
See the solution below.

I didn't want to recompile the assemblies, so my hack is ugly, but affects only javascript and aspx.
Edit /Composite/scripts/compressed/top.js.
Find there SystemTreeNodeBinding.prototype.onBindingAttach=function(){ and inject the following code at the beginning of this function:
if(window.treeNodeProcessor)window.treeNodeProcessor(this.node);
Now you can modify the tree node before it is displayed; you just need to create your global function treeNodeProcessor.
Edit /Composite/top.aspx (top.aspx.cs is affected as well, so save them together).
At the end of the head element add your javascript:
<script type="text/javascript">
function byKey(key) {
return function(p) {
return p.Key == key;
}
}
window.treeNodeTitles = {
<% WriteTreeNodeTitles(); %>
"": ""
};
window.treeNodeProcessor = function(node) {
if(node._data.PropertyBag) {
var uri = node._data.PropertyBag.find(byKey('Uri')).Value;
node._data.Label = window.treeNodeTitles[uri] || node._data.Label;
}
}
</script>
Unfortunately, there's no UrlTitle in the node object passed to treeNodeProcessor. However, every page node has PropertyBag that stores a value like ~/page(a9d30645-02f7-4412-bd4e-6f3a02782481) under key Uri. So, you have to query the UrlTitle on your own, what is made in method WriteTreeNodeTitles (see below).
Edit /Composite/top.aspx.cs.
Add new method:
protected void WriteTreeNodeTitles()
{
using (var conn = new DataConnection())
{
foreach( string line in conn.Get<IPage>().Select( p => " \"~/page(" + p.Id.ToString().ToLower() + ")\": \"" + p.UrlTitle + "\",\r\n" ) )
{
Response.Write( line );
}
}
}
You have to add few usings, of course:
using System.Linq;
using Composite.Data;
using Composite.Data.Types;
So, now your top.aspx contains the mapping of page guid-like urls to UrlTitles, and the treeNodeProcessor function that uses that mapping for modifying your page tree.

Related

'504 - Gateway Timeout' when Indexing the items in Episerver Find

When Indexing the items, it fails sometimes and it gives,
The remote server returned an error: (504) Gateway Timeout. [The remote server returned an error: (504) Gateway Timeout.]
The Indexing logic is here as below,
var client = EPiServer.Find.Framework.SearchClient.Instance;
List<ItemModel> items = getItems(); // Get more than 1000 items
List<ItemModel> tempItems = new List<ItemModel>();
//Index 50 items at a time
foreach(var item in items)
{
tempItems.Add(item);
if (tempItems.Count == 50)
{
client.Index(tempItems);
tempItems.Clear();
}
}
What causes this to happen ?
Note: The above mentioned ItemModel is a custom model which is not implemented interfaces (such as IContent). And the items is a list of ItemModel objects.
Additional info:
EPiServer.Find.Framework version 13.0.1
EPiServer.CMS.Core version 11.9.2
I always figured the SearchClient to be a bit sketchy when manipulating data in Find, as far as I figured (but I have to check this) the SearchClient obey under the request limitation of Episerver Find and when doing bigger operations in loops it tends to time out.
Instead, use the ContentIndexer, i.e.
// Use this or injected parameter
var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
// Remove all children or not
var cascade = true;
ContentReference entryPoint = ...where you want to start
// Get all indexable languages from Find
Languages languages = SearchClient.Instance.Settings.Languages;
// Remove all current instances of all languages below the selected content node
//languages.ForEach(x => ContentIndexer.Instance.RemoveFromIndex(entryPoint, cascade.Checked, x.FieldSuffix));
foreach (var lang in languages)
{
if (cascade)
{
var descendents = loader.GetDescendents(entryPoint);
foreach (ContentReference descendent in descendents)
{
ContentIndexer.Instance.RemoveFromIndex(descendent, false, lang.FieldSuffix);
}
}
// Try delete the entrypoint
var entryTest = loader.Get<IContent>(entryPoint, new CultureInfo(lang.FieldSuffix));
if (entryTest != null)
{
var delRes = ContentIndexer.Instance.Delete(entryTest);
}
}
This is the most bulletproof way to delete stuff from the index as far as I figured.

Unable to cast object of type [TweetSharp.TwitterUser]' to type 'TweetSharp.TwitterCursorList`1[TweetSharp.TwitterUser]'

hi i want to get follower list via tweetsharp but i have a exception this = Unable to cast object of type 'System.Collections.Generic.List1[TweetSharp.TwitterUser]' to type 'TweetSharp.TwitterCursorList1[TweetSharp.TwitterUser]'.
help me please HOW CAN I GET FOLLOWERLIST?
my code like this
string aranan = "anilsarii";
var AramaAyari = new SearchForUserOptions { Q = aranan, Count = 25 };
var users = ts.SearchForUser(AramaAyari); //Get list of users by query
//...
//var asd=ts.FollowList(new FollowListOptions{ OwnerId= 2603023494});
var followers = ts.ListFollowers(new ListFollowersOptions { Cursor = -1 });
while (followers.NextCursor != null)
{
followers = ts.ListFollowers(new ListFollowersOptions { followers.NextCursor });
}
You may just need to change this line;
followers = ts.ListFollowers(new ListFollowersOptions { followers.NextCursor });
to
followers = ts.ListFollowers(new ListFollowersOptions { Cursor = followers.NextCursor });
If that doesn't work, it's likely a problem in TweetSharp itself. TweetSharp has been abandoned by the original authors and is no longer maintained. As a result, there are a number of problems using it with the current Twitter API, due to changes on Twitter's end in the last few years. However there are several forks of TweetSharp that are current.
I tried your code with the one line modified above using TweetMoaSharp (a fork, available on Nuget, or on github here; https://github.com/Yortw/tweetmoasharp) and it worked fine.
Full disclosure: TweetMoaSharp is a fork primarily maintained by me. If you search Nuget for 'TweetSharp' you should find several others.

Circular references and stack overflow exceptions

I have this many to many association between KundeInfo and HovedKategori, which I have mapped in my MS SQL database like this:
I have implemented the methods KundeInfo.HovedKategoris:
public IEnumerable<KundeInfo> KundeInfos
{
get
{
using (var dc = new DataClassesBSMAKSDataContext())
{
dc.DeferredLoadingEnabled = false;
var kundeInfoHovedKategoris = dc.KundeInfoHovedKategoris.Where(x => x.HovedKategori_Id == Id);
var kundeInfos = dc.KundeInfos.Where(x => kundeInfoHovedKategoris.Any(y => y.KundeInfo_Id == x.Id));
return kundeInfos.ToList();
}
}
}
... and HovedKategori.KundeInfos:
public IEnumerable<HovedKategori> HovedKategoris
{
get
{
using (var dc = new DataClassesBSMAKSDataContext())
{
var kundeInfoHovedKategoris = dc.KundeInfoHovedKategoris.Where(x => x.KundeInfo_Id == Id);
var hovedKategoris = dc.HovedKategoris.Where(x => kundeInfoHovedKategoris.Any(y => y.HovedKategori_Id == x.Id));
return hovedKategoris.ToList();
}
}
}
This retrieves the associated KundeInfos from a specific HovedKategori and opposite. The problemhowever lies in the serialization. When I call ToList(), or serialize these objects to JSON, linq will try to first follow all references returned by HovedKategori.KundeInfos, if it were that method I were to call first, and then it would for each returned object, try to follow all references returned by KundeInfo.HovedKategoris and so on, until it would cast a stack overflow exception.
If I could somehow prevent linq from following certain properties with an [Ignore] attribute or something, it would work, but I haven't been able to find anything like that.
What can I do in this situation?
This is in part a design issue. What you should really ask yourself is if you need navigation properties in every possible direction. For example if you just add a Kategori ID instead of a navigation property you could still query your context (by using the ID) but do you really need to always get all the Kategori's with all underlying data?
also if you make your properties virtual you have lazy loading and will only get the information if you .Include it or explicitly reference it.
Ok - So I solved this problem by just making it into methods on the respective classes, which it should be in the first place, since it retrieved these entities from the database. So yes, partially design issue.
Virtual did not work, I considered using projection and an abstract class, but it would be such a haystack of inheritance, and class casts, that it would not even be worth considering.
public IEnumerable<KundeInfo> KundeInfos()
{
using (var dc = new DataClassesBSMAKSDataContext())
{
var kundeInfoHovedKategoris = dc.KundeInfoHovedKategoris.Where(x => x.HovedKategori_Id == Id);
var kundeInfos = dc.KundeInfos.Where(x => kundeInfoHovedKategoris.Any(y => y.KundeInfo_Id == x.Id));
return kundeInfos.ToList();
}
}

Rendering C1 functions in an external page - data missing

I have a sub-application (YetAnotherForum.NET) living in a child directory of my Composite C1 site. In order to maintain a consistent look and feel I want to pull in C1 functions for the navigation elements.
Note: All html mark-up in code below has had pointy brackets replaced with square brackets in order to allow posting here.
I've figured out I can call C1 functions using this syntax:
[f:function ID="Function1" name="Custom.Layout.FooterLinks" runat="server"/]
However, the data behind the function seems to be unavailable. Any ideas what the data issue might be? Perhaps I need the external page to inherit from some form of Composite C1 page?
Here's the function code:
#using Composite.Data;
#using Composite.Data.Types;
#using Composite.Data.ProcessControlled.ProcessControllers.GenericPublishProcessController;
#using CompositeC1Contrib.RazorFunctions;
#inherits CompositeC1WebPage
#functions {
private IEnumerable FooterLinkPages()
{
IEnumerable pages = DataFacade.GetData();
IEnumerable returnPages;
using (DataConnection connection = new DataConnection())
{
returnPages = (from l in connection.Get()
join p in pages on l.Page equals p.Id
where l.PublicationStatus == GenericPublishProcessController.Published
&& p.PublicationStatus == GenericPublishProcessController.Published
orderby l.Position ascending
select p).ToList();
}
return returnPages;
}
}
[ul class="unstyled"]
#foreach (IPage page in FooterLinkPages())
{
[li]#(String.IsNullOrWhiteSpace(page.MenuTitle) ? page.Title : page.MenuTitle)[/a][/li]
}
[/ul]
You need to wrap the data access code in:
using(Composite.Core.Threading.ThreadDataManager.EnsureInitialize())
{
using (DataScope localeScope = new DataScope(new System.Globalization.CultureInfo("en-NZ")))
{
...
}
}

google like search of backbone collection

I'd like to be able to search model attributes contained within a backbonejs collection. This is how I do it now...
wherePartial: function(attrs) {
// this method is really only tolerant of string values. you can't do partial
// matches on objects, but you can compare a list of strings. If you send it a list
// of values; attrs={keyA:[1,2,3],keyB:1}, etc the code will loop through the entire
// attrs obj and look for a match. strings are partially matched and if a list is found
// it's expected that it contains a list of string values. The string values should be considered
// to be like an OR operation in a query. Non-list items are like an AND.
if (_.isEmpty(attrs)) return [];
var matchFound = false;
return this.filter(function(model) {
// this is in the outer for loop so that a function isn't created on each iteration
function listComparator(value, index, list){
return model.get(key).toLowerCase().indexOf(value.toLowerCase()) >= 0;
}
for (var key in attrs) {
if (_.isArray(attrs[key])){
matchFound = _.any(attrs[key],listComparator);
if (matchFound !== true) return false;
} else {
matchFound = model.get(key).toLowerCase().indexOf(attrs[key].toLowerCase()) >= 0;
if (matchFound === false) return false;
}
}
return true;
});
}
Assume "C" is an instantiated collection, this is how I use it:
name:joe (nickname:joe the man nickname:joe cool nickname:joey)
is typed into a textbox and converted into this:
C.wherePartial({name:"joe",nicknames:["joe the man","joe cool","joey"]})
The above method returns all models that have the name joe and within that scope, any of the models that have the name joe and any of the nicknames. It works well for what I use it for. However, I'd really like to make a search that doesn't require the key:value pattern. I'd like to do this in a search box like when using a search engine on the web. I thought about just looking at every attribute on each model, but that takes awhile when you have a large collection (160k+ models).
Has anyone come across a need like this in the past? If so, how did you solve it? I'd like to keep the search contained on the client and not use any ajax calls to the backend. The reason for this is that the entire collection is already loaded on the client.
I thought of a way to do it. Serialize the attributes to a string during model instantiation. Listen for updates and update the serialization.
serializeAttr: function(){
this.serializedAttr = "";
var self = this;
_.each(this.toJSON(),function(value, key, list){
self.serializedAttr += value;
});
}
Then I can do simple searches on that cached value:
cc.serializedAttr.toLowerCase().indexOf("joe") >= 0

Resources