How to get current member in a Razor macro - macros

I have asked this question on the Our Umbraco forums, however I wanted to also increase my chances of getting a solution by posting the same question here.
The issue I have is that, inside a Razor macro, I'm unable to get the current Member who is accessing the site. I have tried the following methods:
Calling Member.GetCurrentMember(), but this returns NULL.
Calling Membership.GetUser(), but this returns NULL
Calling UmbracoEnsuredPage.CurrentUser returned NULL;
Is there another way to get the current Member seeing how the above methods do not work in my case?

var m = Membership.GetUser();
That should work, just verified it myself on 4.7.1; it will return NULL if you are not logged in as a member, but when you log in it should get you what you want.

Just a slight change from #E.J.Brennan if the NULL is an issue you can check if you are logged on before trying to GetUser():
if (umbraco.library.IsLoggedOn())
{
m = Membership.GetUser();
}

Starting from v7 you can use the MembershipHelper
#Members.CurrentUserName
#Members.GetCurrentMember()
#Members.GetCurrentMemberId()

Now it's even easier, in the surfaceController you can use just one line:
var member = ApplicationContext.Current.Services.MemberService.GetById(Members.GetCurrentMemberId());
If Members MembershipHElper isn't accessible:
var memberShipHelper = new MembershipHelper(UmbracoContext.Current);
var member = ApplicationContext.Current.Services.MemberService.GetById(memberShipHelper.GetCurrentMemberId());

Related

FindBy property in TYPO3 Extbase MVC is not working

I'm unable to run the FindBy magic function property in Extbase MVC
$title=array(0 =>'Books Day');
$each_event=$this->eventRepository->findByTitle($title);
$each_event is returning an object of type TYPO3\CMS\Extbase\Persistence\Generic\QueryResult .
How do I make this work?
I also tried passing in a string to findByTitle and findByOne. Both don;t work! :(
I'm using TYPO3 6.1 and extension builder.
The last part of those magic functions always needs to be a field in the database. So "title" must be in your model. You might have a field "one" for your object, but I guess you meant findOneByTitle?
The object type QueryResult is correct. You can turn it into an array for debugging purpose for example:
$foo = $query->execute()->toArray();
By the way: check wether your eventRepository is null or not and you could try this to see if it works at all:
$result = $this->myRepository->findAll();
Try
$each_event=$this->eventRepository->findByTitle($title)->toArray();
Reference to the QueryResult.
As said in the documentation, it returns a QueryResultInterface|array.
As a consequence you have to loop over the result like this:
foreach($each_event as $single_event) {
$single_event->getProperty();
}
If you are sure that it returns only one single value you could also access it by the index 0:
$each_event[0]->getProperty();

Not able to get First Name, Last Name, or Realm Id from Open Id Attributes

According to the SSO documentation for IA these attributes should be available (I'm guessing a bit at the attributes URI):
First Name (http://axschema.org/namePerson/first)
Last Name (http://axschema.org/namePerson/last)
Realm Id (http://axschema.org/intuit/realmId)
Reviewing the query string passed during stage 3 of the open id request, here are the attributes present:
openid.alias3.type.alias1 => http://axschema.org/namePerson
openid.alias3.value.alias1 => Full Name
openid.alias3.type.alias2 => http://axschema.org/contact/email
openid.alias3.value.alias2 => email#test.com
Bug, error in the documentation, or loose nut behind the keyboard?
Two problems here, the first problem is in my haste of cut and paste coding I was only requesting the full name and email. I revised the code to request first name, last name, and realm id. Now first name and last name come through fine. However, it took a big of poking around to get to the bottom of the realm id issue. First, the documentation did not give a clear answer on the attribute uri; however, I was able to find a clear answer on this thread https://idnforums.intuit.com/textthread.aspx?catid=69&threadid=16954. Paul Jackson gives a clear idea what is going on here:
The attribute for realm id is http://axschema.org/intuit/realmId
Sometimes the attribute does not come through
I put together a technique based on his suggestion in this thread. Basically, if the realm id does not come through then I'll parse it from the referring url which has it in the query string as realmId. Clearly, this is brittle but provides a "working" solution for now.
Here is a code snippet you can use during stage 3 of the handshake.
_realmId = fetch.GetAttributeValue(OpenId.IntuitWellKnownRealmId);
if (_realmId == null && httpRequest.UrlReferrer != null)
{
var url = httpRequest.UrlReferrer.ToString();
var i = url.IndexOf('?');
if (i != -1)
{
var querystring = url.Substring(i);
_realmId = System.Web.HttpUtility.ParseQueryString(querystring)["realmId"];
}
}
I take zero credit for this solution, Paul already had it figured out. Just posting here to help anyone searching on this problem.

Sending an array as a query parameter to a rest WS (nullpointerException)

I need to call a rest web service in playframework 2 and I need to send a query parameter which is an array. In regular html I would send it like:
GET http://host.com?a=1&a=2&a=3
But when I do it when I try to do it with the playframework 2 WS api I do the next:
Map<String,String[]> paramMap = new HashMap<String, String[]>();
paramMap.put("a",new String[]{"value1","value2"});
WS.WSRequestHolder holder = WS.url("http://host.com");
Set<String> keys = paramMap.keySet();
for (int i = 0; i < paramMap.get(key).length; i++)
{
holder.setQueryParameter(key, paramMap.get(key)[i]);
}
And the first time that setQueryParamater() arrives, everything goes perfect but the second time I get a NullPointerException and paramMap.get(key)[i] is not null. Is this possible? is there any workaround?
Thanks in advance!
In regular html you need to dsend it like:
GET http://host.com?a=1&a=2&a=3
Using ampershand(&) to separate parameters in query string instead of comma(,)
Edit: Sorry forgot to type: Like ur trying to do within the loop you should not have fields with same name. You should change it as:
http://host.com?a1=value1&a2=value2
I don't know if this causes NullPointerException you face. But even if does not i suggest you change ur implementation to keep one value per field (key, value)
Ok, it seems to be a bug in play-framework, but solved in 2.1 version.
https://play.lighthouseapp.com/projects/82401/tickets/360-bug-in-wsjava-setqueryparameter-leads-to-npe-when-adding-a-query-parameter-twice

umbraco.library.NiceUrl() returns error when passing a content node id

I have created a doctype in umbraco which has one of the following property:
Property - Case study link
Datatype - Content picker
I need to fetch the URL of this document in a Razor macro and assign it to a hyperlink.
Currently am doing it in this way but it's giving me an error:
#foreach (var item in #Model.OurWork){
Read case study
}
And here is the error I get on viewing the page:
Error loading Razor Script OurWorkGrid.cshtml The best overloaded
method match for 'umbraco.library.NiceUrl(int)' has some invalid
arguments
I have tried outputting the node id without using the niceURL() function and it works fine (outputs 1088).
Read case study
results in this:
Read case study
But as soon as I put back NiceURL() function, it chokes again.
I really don't know what am I doing wrong here!
Instead of using the umbraco library method, try loading the node with the ID first, and then using the Url property to get the nice URL.
#foreach (var item in #Model.OurWork){
var caseStudyNode = #Model.NodeById(item.caseStudyLink);
Read case study
}
Also, add some form of a check to make sure the value is set, in case it's not a mandatory property on the doc type. Here's one example:
if (#item.HasProperty("caseStudyLink") && !string.IsNullOrEmpty(#item.caseStudyLink))
{
...
}
Try something like:
#foreach (var item in #Model.OurWork){
Read case study
}
You may want to check first whether item.caseStudyLink contains a value because this will throw an error otherwise.

JIRA SOAP Client - return invalid field value

I have a problem using JIRA SOAP client. When I use RemoteIssue.getResolution() or RemoteIssue.getStatus() I get number values. For example let say we have an issue that has resolution = fixed. When I call client.getIssueFromJqlSearch(token, "issuetype = Bug AND resolution = fixed", 10) this will return an array of type RemoteIssue[]. Now if I call issues[i].getResolution() (where issues[] is the result from previous call getIssuesFromJqlSearch) this will return a value "5" instead of "fixed". How to solve this? Is there any way to get the value "fixed" for that issue and not a custom value "5"?
Thank you in advance.
If you call getResolutions() on the SOAP API you'll get an array of all the resolutions defined in the instance, including their Ids. You can then use this to work out which resolution the Id you've got refers to.