How to retrieve a user profile picture in Liferay - liferay

I want to retrieve a user profile picture. How do i do it? Could you please share a code snippet? Im using Liferay 6.0.6. It has only user.getPortraitId() and no user.getPortraitURL(). So once i get the portrait id inside a JAVA class, what do i do with it?

See the implementation of UserConstants.getPortraitURL(...)
https://github.com/liferay/liferay-portal/blob/master/portal-service/src/com/liferay/portal/model/UserConstants.java
On this approach you can get the image url.
If you need the image object, you can load it with ImageLocalServiceUtil:
long portraitId = user.getPortraitId();
Image image = ImageLocalServiceUtil.getImage(portraitId);

There are at least two options on rendering portraits in JSP:
<img src="<%= themeDisplay.getPathImage()%>
/image_gallery?img_id=<%= image.getImageId()%>&t=
<%= ImageServletTokenUtil.getToken(image.getImageId())%>">
<img src="<%= themeDisplay.getPathImage() %>/user_portrait?img_id=<%=id %>">
The first approach contains additional security aspect based on security token which you may or may not find relevant to your needs.

Related

Cannot identify the object in Selenium

I cannot identify the object of the icon showed in the attached screen shot. I have shown the HTML code as well.
The ID's are getting changed dynamically.
Can anyone guide on how to identity this kind of objects in Selenium?
If the ID is always changed, I recommend using CssSelector instead.
For instance,
<div id="running_number_12345" class="icon something">...</div>
You can use locator
driver.FindElement(By.CssSelector("div[class*='icon something']"));
If your icon doesn't have any specific css pattern, I recommend adding something in class attribute. If not, you have to use complex CssSelector to find it.
Try this
driver.findElement(By.cssSelector(".icon something"));

Context path not working in liferay Hook

I am creating a hook in which i want to add my image in custom jsp.
I have added image inside docroot/images/1.jpg
in jsp i am trying to access it using
<img src="<%=request.getContextPath()%>/images/1.jpg" />
but i am not able to get image.
Additionally when i am trying to print <%=request.getContextPath()%> is only prints "/"
Thanks !!!
Instead of taking images from your war (docroot folder), suggest you to place the images in a Web server so that in future you can easily replace them if you want to change images.
You could use below code in your jsp page to retrieve from Web server,
<div class="yourCSSClass">
<liferay-ui:icon src="/yourWebServerPath/1.jpg" label="" message=""/>
</div>
Another flavour of it,
In your JSP page if you have below code,
<nav class="yourCSSClass"><br/><br/></nav>
Then, in the CSS file that you are importing in your JSP page, use this,
.yourCSSClass{
background: url(yourWebServerPath/1.jpg) no-repeat;
}
P.S : In both the above examples, decide properly the value for yourWebServerPath
Very old question, but still worth answering.
Your hook replaces portal’s JSPs with your owns but that doesn’t mean that the context of the new JSPs is your hook’s one.
What actually happens is that JSPs are replaced at the File System level, and called by other Liferay JSPs and configuration as they were the original one.
The request you have access to, therefore, is the main and original one.
As far as I know, there’s no clean way to get the context path of the hook that is providing an overriding JSPs.

rendering Liferay page URLs inside of portlets Liferay 6.1

I'm new to liferay and I'm almost positive this is blazingly simple to do: Using velocity markup, I want to be able to generate links to pages within my Liferay website and embed them inside of my portlets on different pages.
I have a vague idea of how it might be done so I searched around figuring it would be posted somewhere, but I can't find anything on it. Incidentally, I want to put whatever code I come up with inside the view.jsp of the portlet. I would use velocity markup here but I don't think (don't know for sure) if that is allowed inside of a jsp.
Please let me know if you need more information to respond.
I would use velocity markup here but I don't think (don't know for sure) if that is allowed inside of a jsp.
Why would you want to use Velocity mark-up inside a JSP (view.jsp)? I don't see any advantages in doing that apart from the argument that you are really great at velocity.
Though here is a link that would help you embed velocity inside of JSP.
Note: In my opinion it is not a good practice to embed velocity within JSP in a portlet
In JSP:
You will need a Layout object which you can get with the help of static methods in LayoutLocalServiceUtil.
After you get the Layouts, you can use the static methods of com.liferay.portal.util.PortalUtil like getLayoutFriendlyURL or getLayoutFullURL etc to build the URL.
In VM (these would be *.vm files in themes):
You can follow all the same steps as mentioned in JSP. The things you would need to do that are:
Instance of LayoutLocalService, can be found out by using the following code (taken from this answer):
#set($layoutLocalService = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))
now you can use the velocity variable $layoutLocalService to make calls to service methods for getting the layouts.
Then you can call methods of PortalUtil class by using the variable $portalUtil available for *.vm files in themes.
You can check-out the following files for more details (if you are interested):
docroot/html/themes/_unstyled/templates/init.vm, this contains all the velocity variables available in themes. Variables of interest might be $theme, $theme_display, $layout, $navItems.
docroot/html/themes/_unstyled/templates/portlet.vm, this file is a template to display the individual portlets.
docroot/html/themes/_unstyled/templates/navigation.vm, contains code for displaying the navigation menu with page links.
docroot/html/themes/_unstyled/templates/portal_normal.vm, this file represents a page-template in liferay and this contains the other files like navigation.vm & portlet.vm.
For Velocity:
Okay so for generating the links for Liferay pages in velocity take a look at the following file in the Liferay source code:
/portal-web/docroot/html/themes/_unstyled/templates/navigation.vm
In there you'll see how the default Liferay theme generates the navigation structure for your site. To make life easier for you here it is:
<nav class="$nav_css_class" id="navigation">
<h1>
<span>#language("navigation")</span>
</h1>
<ul>
#foreach ($nav_item in $nav_items)
#if ($nav_item.isSelected())
<li class="selected">
#else
<li>
#end
<a href="$nav_item.getURL()" $nav_item.getTarget()><span>$nav_item.icon() $nav_item.getName()</span></a>
#if ($nav_item.hasChildren())
<ul class="child-menu">
#foreach ($nav_child in $nav_item.getChildren())
#if ($nav_child.isSelected())
<li class="selected">
#else
<li>
#end
<a href="$nav_child.getURL()" $nav_child.getTarget()>$nav_child.getName()</a>
</li>
#end
</ul>
#end
</li>
#end
</ul>
So the Velocity is looking through a collection called $nav_items, and then calls the getURL() method on each item to generate the link.
For JSP:
You'll need to make use of the LayoutLocalServiceUtil class, and in
particular one of the getLayouts() methods.You'll have to pick the one that best suits your need.
This will return the list of Layouts (your pages), and then you can
call getFriendlyURL() on each of these layouts to return it's url
This will be a relative url to your site, so something like
/my-site-home-page.
Let me know if you have any more questions!

Using resource files in SharePoint MasterPages

I haven't been able to figure out how to use strings from resource files (resx) in SharePoint master pages.
I know how to use it with server controls, but can I somehow extract a value and use it in generalt html. I.e. in an alt attribute on a img tag?
<img src="photo.jpg" alt="my_resource_entry_here" />
Here are 2 very good blog posts that describe how to use resources:
http://tomblog.insomniacminds.com/2008/02/25/sharepoint-internals-resources/
http://www.mikhaildikov.com/2007/03/sharepoint-resources-types-use-and_2163.html
The easiest way would be to add a runat="server" attribute to your alt tag. That will solve your problem and you can use the "normal" way of getting resources.
Othwerwise use this syntax to get value from the resx files places in App_Global resources:
<img src="photo.jpg"
alt='<%=this.GetGlobalResourceObject("Global", "Mystring").ToString()%>'
I assume you have a class with a method that lets you get the resource string by the key, e.g. MyResources.GetString(key). In that case you can use something like this:
<img src="photo.jpg" alt='<%=MyResources.GetString("my_resource_key_here")%>' />

SharePoint 2007 - How To Change Attachment Paperclip Image

When a list item has an attachment, SharePoint automatically renders a paperclip image for that particular row, which indicates that the item has an attachment. Is there any way to change the image that is rendered?
The site is in a shared hosting environment, so I can't simply replace the image on the file system. Also, there are other lists that are part of the same site that should use the default image.
Is there any way to change the image that is rendered for items with an attachment on an individual list basis?
EDIT: Following is the HTML that is rendered:
<td class="ms-vb2">
<img align="absbottom" src="http://devsandbox/_layouts/images/attach.gif" alt="Attachment"/>
</td>
The only real way you'll be able to do this is to use jQuery (or some other javascript library). You'll need to locate the elements you want to update on the page and change the URL's
$('img[src*=attach.gif]').each(function() {
$(this).attr('src', '/path/to/new/image.png');
}
My jQuery may be a touch wrong but that should be near enough to give you an idea of what to do
Edit - The best way to have this down would be via a custom WebPart which renders the JavaScript. This way it can easily be dropped into any page you want
i don't know which element off the top, but I would look for it in one of the stylesheets and use SharePoint designer to do the replacement work for a specific list.
Use Firebug to inspect the element that you want to revert. This will tell you the css class and other properties used by that element. Then write your own class and add it to the core.css file or if you want, add it to the css for the Site/Site Collection through the MasterPages link in Site Settings.
EDIT
I used firebug to look at an image in SharePoint and here is how it is rendering.
<img id="img_1-2_" class="rpo-gif rpo-gif-2" border="0" style="padding: 0px;" alt="Expand/Collapse" src="/_layouts/images/minus.gif"/>
As you can see it is using a class and setting a src to '/_layouts/images/minus.gif'. Well you can go find that file in the 12Hive directory and then replace it with an image of your choice that has the same name.

Resources