How to add nested portlets(liferay) through code - liferay

We have something called nested portlets in liferay. I want to add this portlet dynamically through code. Does anyone know the code for adding nested portlets, and add other portlets inside it?
Thanks !!!

for complete example i'll assume that you want to add nested portlet to current page using another portlets action handler. (if used from render action you would not see nested portlet until next view of the page)
Add these methods to your code
private static String addPortlet(final long p_userId, final Layout p_layout, final String p_portletId, final String p_columnId, final int p_position, final boolean p_checkPermission)
throws PortalException, SystemException
{
if (p_layout.isTypePortlet()) {
final LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) p_layout.getLayoutType();
final String portletId = layoutTypePortlet.addPortletId(p_userId, p_portletId, p_columnId, p_position, p_checkPermission);
if (portletId != null) {
final String rootPortletId = PortletConstants.getRootPortletId(portletId);
final String portletPrimaryKey = PortletPermissionUtil.getPrimaryKey(p_layout.getPlid(), portletId);
ResourceLocalServiceUtil.addResources(p_layout.getCompanyId(), p_layout.getGroupId(), 0, rootPortletId, portletPrimaryKey, true, true, true);
LayoutLocalServiceUtil.updateLayout(p_layout.getGroupId(), p_layout.isPrivateLayout(), p_layout.getLayoutId(), p_layout.getTypeSettings());
}
return portletId;
}
return null;
}
private static void addNestedPortlet(final PortletRequest p_request) throws PortalException, SystemException {
final ThemeDisplay themeDisplay = (ThemeDisplay) p_request.getAttribute(WebKeys.THEME_DISPLAY);
final Layout layout = themeDisplay.getLayout();
long userId = themeDisplay.getUserId();
//create nested portlet and add it to "column-1"
final String nestedPortletId = addPortlet(userId, layout, "118", "column-1", -1, false);
//this will be used to target nested portlet's columns
final String nestedColumnPrefix = "_" + nestedPortletId + "__";
//default page layout (used by nested portlet) has two columns
//we'll add two portlets (in this example two iframe portlets), one portlet to each column
addPortlet(userId, layout, "48", nestedColumnPrefix + "column-1", -1, false);
addPortlet(userId, layout, "48", nestedColumnPrefix + "column-2", -1, false);
}
If you would like, and probably you would, to add nested portlet to another page or not from portlet, you can lookup Layout and User instead of getting them from ThemeDisplay.

Related

Liferay control panel menu portlets

How does liferay list out the portlet to be shown in the control panel menu?
Can any body list the classes and JSP involved in the same?
Refer to file in your liferay source at location : \portal-web\docroot\html\portlet\control_panel_menu\view.jsp
You will get idea how liferay shows its control panel menu.
Link for code of this jsp.
All of the portlets are saved in the portlet table while we deploy them. Then once the control panel is accessed then liferay loads all of the portlet which are having the control panel entry in them. For better explanation see the below code in
com.liferay.portal.util.PortalImpl
#Override
public Set<Portlet> getControlPanelPortlets(long companyId, String category)
throws SystemException {
Set<Portlet> portletsSet = new TreeSet<Portlet>(
new PortletControlPanelWeightComparator());
if (Validator.isNull(category)) {
return portletsSet;
}
List<Portlet> portletsList = PortletLocalServiceUtil.getPortlets(
companyId);
for (Portlet portlet : portletsList) {
String portletCategory = portlet.getControlPanelEntryCategory();
if (category.equals(portletCategory) ||
(category.endsWith(StringPool.PERIOD) &&
StringUtil.startsWith(portletCategory, category))) {
portletsSet.add(portlet);
}
}
return portletsSet;
}
#Override
public List<Portlet> getControlPanelPortlets(
String category, ThemeDisplay themeDisplay)
throws SystemException {
Set<Portlet> portlets = getControlPanelPortlets(
themeDisplay.getCompanyId(), category);
return filterControlPanelPortlets(portlets, themeDisplay);
}
The above code is called from the
\portal-web\docroot\html\portlet\control_panel_menu\view.jsp
Map<String, List<Portlet>> siteAdministrationCategoriesMap = PortalUtil.getSiteAdministrationCategoriesMap(request);
Help taken from pankaj-kathiriya answer. Thanks for the same.

Save values between page navigation in Windows Phone

How do I save values between page navigation in windows phone,
suppose I have two text blocks in my one phone application page, and they contains dynamically changing values every time, now suppose my text block have value "abc" and for some reason I go back to previous page, now when I get back on my page, I want that text block having value "abc". How to do it??
There are several methods available
IsolatedStorageSettings
Save
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
// txtInput is a TextBox defined in XAML.
if (!settings.Contains("userData"))
{
settings.Add("userData", txtInput.Text);
}
else
{
settings["userData"] = txtInput.Text;
}
settings.Save();
Read
if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
{
txtDisplay.Text +=
IsolatedStorageSettings.ApplicationSettings["userData"] as string;
}
PhoneApplicationService.Current.State
PhoneApplicationService.Current.State["param"] = param
and on other page we can get it like this.
var k = PhoneApplicationService.Current.State["param"];
Define two static variable in your App.xaml.cs
public static valueOne = string.Empty;
public static valueTwo = string.empty;
//Assign textbox value to variable on page leaving event
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
if(!string.IsNullOrEmpty(txtBoxOne.Text))
App.valueOne = txtBoxOne.Text;
if(!string.IsNullOrEmpty(txtBoxTwo.Text))
App.valueTwo = txtBoxTwo.text;
}
//Get value from page load
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if(!string.IsNullOrEmpty(App.valueOne))
string valueFirst = App.valueOne;
if(!string.IsNullOrEmpty(App.valueTwo ))
string valueTwo = App.valueTwo ;
}
There are various approaches to solve this.
Common thing is using a Static Class, which holds static properties and binding it to your View.

How can I remove the tables rendered around the webparts in the Rich Content area?

How would I override the tables rendered around the webparts in the "Rich Content" area?
I have successfully removed the tables around webpartzones and their webparts but can't figure how to remove the tables around Rich Content area webparts.
I am not using the Content Editor WebPart.
The "Rich Content" area I am using is created using the PublishingWebControls:RichHtmlField.
This is the control which has content and webparts.
Bounty here.
I have pondered this myself in the past and I've come up with two options, though none are very appealing, so have not implemented them:
Create a custom rich text field. Override render, call base.Render using a TextWriter object and place the resulting html in a variable, which you then "manually" clean up, before writing to output.
Create a custom rich text field. Override render, but instead of calling base.Render, take care of the magic of inserting the webparts yourself. (This is probably trickier.)
Good luck!
Update, some example code I use to minimize the output of the RichHtmlField:
public class SlimRichHtmlField : RichHtmlField
{
protected override void Render(HtmlTextWriter output)
{
if (IsEdit() == false)
{
//This will remove the label which precedes the bodytext which identifies what
//element this is. This is also identified using the aria-labelledby attribute
//used by for example screen readers. In our application, this is not needed.
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
base.Render(htw);
htw.Flush();
string replaceHtml = GetReplaceHtml();
string replaceHtmlAttr = GetReplaceHtmlAttr();
sb.Replace(replaceHtml, string.Empty).Replace(replaceHtmlAttr, string.Empty);
output.Write(sb.ToString());
}
else
{
base.Render(output);
}
}
private string GetReplaceHtmlAttr()
{
return " aria-labelledby=\"" + this.ClientID + "_label\"";
}
private string GetReplaceHtml()
{
var sb = new StringBuilder();
sb.Append("<div id=\"" + this.ClientID + "_label\" style='display:none'>");
if (this.Field != null)
{
sb.Append(SPHttpUtility.HtmlEncode(this.Field.Title));
}
else
{
sb.Append(SPHttpUtility.HtmlEncode(SPResource.GetString("RTELabel", new object[0])));
}
sb.Append("</div>");
return sb.ToString();
}
private bool IsEdit()
{
return SPContext.Current.FormContext.FormMode == SPControlMode.Edit || SPContext.Current.FormContext.FormMode == SPControlMode.New;
}
}
This code is then used by your pagelayout like this:
<YourPrefix:SlimRichHtmlField ID="RichHtmlField1" HasInitialFocus="false" MinimumEditHeight="200px" FieldName="PublishingPageContent" runat="server" />
Got it:
https://sharepoint.stackexchange.com/a/32957/7442

Unable to retain value of a label during post back(button click) in sharepoint

While writing code for connecting two webparts,I created a two labels. the text value for both the labels were written in OnPreRender method. However, i forgot to add control for one label in CreateChildControl method.
So while debugging, i noticed that, after post back, the label whose control i forgot to add didn't retain the value and it was showing empty string.But the other label who's control i added was able to retain the value.
protected override void CreateChildControls()
{
base.CreateChildControls();
btnup.Text = " Update";
this.Controls.Add(lblid);//**If i add this, the label retains the value during post back , otherwise its null**
this.Controls.Add(lblname);
this.Controls.Add(lbldesig);
this.Controls.Add(tbdes);
this.Controls.Add(lblcomp);
this.Controls.Add(tbcomp);
this.Controls.Add(btnup);
btnup.Click += new EventHandler(btnup_Click);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (connectionInterface != null)
{
id = connectionInterface.parameter1;
SPWeb mysite = SPContext.Current.Web;
SPList mylist = mysite.Lists["UpdateList"];
SPListItemCollection itemcol = mylist.Items;
foreach (SPListItem itm in itemcol)
{
string nm = itm["Company_Id"].ToString();
if (nm.Equals(id))
{
lblid.Text = itm["Company_Id"].ToString();
lblname.Text = itm["Name"].ToString();
l
}
}
}
else
{
lblname.Text = "nothing is recieved!";
}
}
Why is it behaving like this?
This is a normal behavior. If you don't add the control to Controls collection, ASP.NET framework will not preserve its value in postback and hence will be lost during postback.

Import javascript and css in head tag in a custom jsf component

How to import a javascript and a css file in head tag of a html generated by a custom jsf component? I've found some articles about using resources for this purpose.
As in (source: http://technology.amis.nl/blog/6047/creating-a-custom-jsf-12-component-with-facets-resource-handling-events-and-listeners-valueexpression-and-methodexpression-attributes):
protected void writeScriptResource(
FacesContext context,
String resourcePath) throws IOException
{
Set scriptResources = _getScriptResourcesAlreadyWritten(context);
// Set.add() returns true only if item was added to the set
// and returns false if item was already present in the set
if (scriptResources.add(resourcePath))
{
ViewHandler handler = context.getApplication().getViewHandler();
String resourceURL = handler.getResourceURL(context, SCRIPT_PATH +resourcePath);
ResponseWriter out = context.getResponseWriter();
out.startElement("script", null);
out.writeAttribute("type", "text/javascript", null);
out.writeAttribute("src", resourceURL, null);
out.endElement("script");
}
}
private Set _getScriptResourcesAlreadyWritten(
FacesContext context)
{
ExternalContext external = context.getExternalContext();
Map requestScope = external.getRequestMap();
Set written = (Set)requestScope.get(_SCRIPT_RESOURCES_KEY);
if (written == null)
{
written = new HashSet();
requestScope.put(_SCRIPT_RESOURCES_KEY, written);
}
return written;
}
static private final String _SCRIPT_RESOURCES_KEY =
ShufflerRenderer.class.getName() + ".SCRIPTS_WRITTEN";
However besides writing in head tag, the import link is also write in my component code. The method writeScriptResource() is called in the beginning of encodeBegin method.
Does anyone know how to do that in order to avoid inject inline scripts and styles in my page?
Thanks in advance,
Paulo S.
I don't know if you can use JSF 2 but with composite components is easier to include resources using h:outputStylesheet and h:outputScript. When the page is rendered, resources will be in the head of the html.

Resources