I have to make one of the more dreaded operations in Umbraco 4.5.2 ;) I have to list all the members who's related (using Relationship API) to the current node. What I have done now is, that I got all the related member objects and setting a Repeater's datasource to this list.
It works, but as we all know, the member api have quite an overhead when when getting properties and such and I haven't found a clever way to access the properies of the member type to display in the repeater template yet.
So, after reading a bit here on the forums, it seems the "best" solutions is to query the database directly to get the members as XML. While that sounds like a better solution than using the Member.GetAllAsList(); I must admit that I'm not that hooked on the more complex sql queries.
Has anyone got any experience with this?
I have found some SQL query examples on how to get the current member, but I'm honestly having some trouble to see what is really going on.
Well, after a bit of arguing with myself I decided to do it in a way which works and which is not querying the db to get *all* members.
I got a collection of members related to the current page like this:
Node currentNode = Node.GetCurrent();
RelationType member2doc = RelationType.GetByAlias("member2doc");
List<Member> relatedMembers = new List<Member>();
List<Relation> relations = Relation.GetRelationsAsList(currentNode.Id);
foreach (Relation r in relations)
{
if (Relation.IsRelated(r.Parent.Id, currentNode.Id, member2doc))
{
relatedMembers.Add(new Member(r.Parent.Id));
}
}
List<MemberProfile> memberProfiles = new List<MemberProfile>();
foreach (Member m in relatedMembers)
{
memberProfiles.Add(new MemberProfile()
{
Address = m.getProperty("adresse").Value.ToString(),
City = m.getProperty("by").Value.ToString(),
Homepage = m.getProperty("hjemmeside").Value.ToString(),
Name = m.getProperty("navn").Value.ToString(),
Phone = m.getProperty("telefonNummer").Value.ToString(),
Zip = m.getProperty("postNummer").Value.ToString()
});
}
The MemberProfile class is a class I made under the usercontrol class which looks as simple as this:
public class MemberProfile
{
public string Name { get; set; }
public string Address { get; set; }
public string Zip { get; set; }
public string City { get; set; }
public string Homepage { get; set; }
public string Phone { get; set; }
}
It's quite a quick'n'dirty fix, but it seems to work ;)
How about using a bidirectional relation type for member2doc ? (since the object types differ, you can be sure that there won't be member IDs in the child field, nor doc IDs in the parent field) that way you can avoid having to return all relations for the current node
and checking each to see if it's a member that matches (each call to IsRelated is a DB hit)
foreach (Relation relation in Relation.GetRelations(currentNode.Id, member2doc)) { relatedMembers.Add(new Member(relation.Parent.Id); }
Listing members in frontend
Hi all,
I have to make one of the more dreaded operations in Umbraco 4.5.2 ;) I have to list all the members who's related (using Relationship API) to the current node. What I have done now is, that I got all the related member objects and setting a Repeater's datasource to this list.
It works, but as we all know, the member api have quite an overhead when when getting properties and such and I haven't found a clever way to access the properies of the member type to display in the repeater template yet.
So, after reading a bit here on the forums, it seems the "best" solutions is to query the database directly to get the members as XML. While that sounds like a better solution than using the Member.GetAllAsList(); I must admit that I'm not that hooked on the more complex sql queries.
Has anyone got any experience with this?
I have found some SQL query examples on how to get the current member, but I'm honestly having some trouble to see what is really going on.
Thanks in advance!
Well, after a bit of arguing with myself I decided to do it in a way which works and which is not querying the db to get *all* members.
I got a collection of members related to the current page like this:
The MemberProfile class is a class I made under the usercontrol class which looks as simple as this:
It's quite a quick'n'dirty fix, but it seems to work ;)
Hi Bo,
How about using a bidirectional relation type for member2doc ? (since the object types differ, you can be sure that there won't be member IDs in the child field, nor doc IDs in the parent field) that way you can avoid having to return all relations for the current node and checking each to see if it's a member that matches (each call to IsRelated is a DB hit)
foreach (Relation relation in Relation.GetRelations(currentNode.Id, member2doc))
{
relatedMembers.Add(new Member(relation.Parent.Id);
}
HTH,
Hendy
You're absolutely right, Hendy :) well-spotted there! Thanks a lot!
is working on a reply...