Liferay 6.2: Creating Page programmatically template not applying - liferay

I have a problem with creating a page programmatically with a page template. The creation of the page is working, but it is not applying the page template to the page. I have searched and found always the same code snippets. There is also no error.
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
long userId = themeDisplay.getUserId();
long groupId = themeDisplay.getScopeGroupId();
boolean privateLayout = false;
long parentLayoutId = 0;
String name = "Testpage";
String title = null;
String description = null;
String type = LayoutConstants.TYPE_PORTLET;
boolean hidden = false;
String friendlyURL = "/testpage";
ServiceContext serviceContext = new ServiceContext();
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
Group group = themeDisplay.getScopeGroup();
serviceContext.setScopeGroupId(group.getGroupId());
serviceContext.setUserId(themeDisplay.getUserId());
Layout layout = LayoutLocalServiceUtil.addLayout(userId, groupId, privateLayout, parentLayoutId, name, title, description, type, hidden, friendlyURL, serviceContext);
List<LayoutSetPrototype> lsps = LayoutSetPrototypeServiceUtil.search(themeDisplay.getCompanyId(), Boolean.TRUE, null);
for(LayoutSetPrototype lsp : lsps) {
System.out.println("->" + lsp.getName() + " - " + lsp.getLayoutSetPrototypeId());
if (HtmlUtil.escape(lsp.getName()).contains("kDSSMP")){
System.out.println("hit");
LayoutTypePortlet ltp = (LayoutTypePortlet) layout.getLayoutType();
ltp.setLayoutTemplateId(userId, lsp.getUuid(), false);
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(),layout.getLayoutId(), layout.getTypeSettings());
addResources(layout, PortletKeys.DOCKBAR);
}
}

Basic question (please correct if I'm in wrong): "hit" is printed and template is not applied?
Just a background question: do you know that LR uses Layout for referring a page and template to indicate a page template? (I think yes)
(If my assertion about "hit" is correct) I don't try your code... but at first look it seems correct.
I have a similar snippet of code (for LR 6.1) in which I wrote:
layoutTypePortlet.setLayoutTemplateId(user.getUserId(), layoutTemplate);
LayoutLocalServiceUtil.updateLayout(layout);
The only differences seem to be on the setLayoutTemplateId (but false is correct to don't check for permissions) and on LayoutLocalServiceUtil.updateLayout.
On the last point Javadoc says:
updateLayout(Layout layout): Updates the layout in the database or adds it if it does not yet exist.
and
updateLayout(long groupId, boolean privateLayout, long layoutId, String typeSettings): Updates the layout replacing its type settings.
The last note is about lsp.getUuid() using... are you sure it is correct?
In javadoc it is declared as "newLayoutTemplateId" and not UUID.

Use LayoutPrototype instead of LayoutSetPrototype. This is Page Template vs. Site Template.

Related

Getting Active View Object Parameters?

I am new to the API and I'm trying to get values from the active view. I am using the following code as a mock up to what I'm trying to do:
public void GetViewProperties()
{
String viewname;
String typename;
String levelname;
String Output;
ViewFamilyType VfamType;
Level lev;
//Get document and current view
Document doc = this.ActiveUIDocument.Document;
View currentView = this.ActiveUIDocument.ActiveView;
//Find the view family type that matches the active view
VfamType = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType))
.Where(q => q.Name == "1-0-Model").First() as ViewFamilyType;
//Find the level that matches the active view
lev = new FilteredElementCollector(doc).OfClass(typeof(Level))
.Where(q => q.Name == "00").First() as Level;
//Get the view's current name
viewname = currentView.Name.ToString();
//Get the name of the view family type
typename = VfamType.Name;
//Get the name of the level
levelname = lev.Name.ToString();
//Combine results for task dialog
Output = "View: " + viewname + "\n" + typename + "-" + levelname;
//Show results
TaskDialog.Show("View Properties Test",Output);
}
I'm cheating at the moment by grabbing the view type and level by name. I really want them to be found by looking at the properties of the active view. I can't figure out how I am meant to access the view type and level name properties. I need to make lambda use a variable e.g. (q => q.Name == Level.name), (q => q.Name == ViewFamilyType.name).
Thanks in advance!
Here is your code corrected:
public void GetViewProperties()
{
//Get document and current view
Document doc = this.ActiveUIDocument.Document;
View currentView = this.ActiveUIDocument.ActiveView;
//Find the view family type that matches the active view
var VfamType = (ViewFamilyType)doc.GetElement(currentView.GetTypeId());
//Find the level that matches the active view
Level lev = currentView.GenLevel;
//Get the view's current name
string viewname = currentView.Name;
//Get the name of the view family type
string typename = VfamType.Name;
//Get the name of the level
string levelname = lev.Name;
//Combine results for task dialog
string Output = "View: " + viewname + "\n" + typename + "-" + levelname;
//Show results
TaskDialog.Show("View Properties Test", Output);
}
You don't need to use a FilteredElementCollector to get these informations. And if you need elsewhere, you don't need a Where: just put your lambda in the First:
new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType))
.First(q => q.Name == "1-0-Model")
If you need to access in your lambda a property specific to a class, not defined on Element, you can use Cast:
new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType))
.Cast<ViewFamilyType>().First(vft => vft.IsValidDefaultTemplate)
And please do not declare all your variable at the start of your methods. You're not writing Pascal. Declare variables as close to the first spot that you use them as possible. It makes your code much more readable. The closer a variable is declared to where it is used, the less scrolling/searching you have to do when reading the code later and it also naturally narrow their scope.
You're probably looking for View.GenLevel property. This will work for views related to levels, such as Plan Views. Note that if this View is not generated by a level, this property is null.

Creating a case in plugin, want to use its ticketnumber immediately

I have a plugin where i am creating a new case and I want to send an email out as it is created including its ticketnumber. I have attempted just to call this in the plugin but it is coming back saying that it is not present in the dictionary. I know this field is populated using CRM's own autonumbering so what i'm guessing is happening is that my plugin is firing and creating the case but then i'm trying to use this field before the autonumber has completed.
So is there a way that i can get my plugin to "wait" until this field is available and then use it?
Thanks
EDIT: Code below:
string emailBody = entity.Attributes["description"].ToString();
int bodyLength = emailBody.Length;
int textStart = emailBody.IndexOf(">") + 1;
int newLength = bodyLength - (textStart + 7);
string description = emailBody.Substring(textStart, newLength);
//create complaint
Entity complaint = new Entity("incident");
complaint["description"] = description;
complaint["ts_case_type"] = 717750001;
complaint["ts_registration_datetime"] = DateTime.Now;
complaint["ownerid"] = Owner;
complaint["customerid"] = Organisation;
Service.Create(complaint);
As a side I would suggest sending the email with a workflow if possible, it will be far easier to maintain in the long run and quicker to implement in the short.
In any case to answer your question, from what you have here you need to update your code to retrieve the ticketnumber once you have created the incident. You can do this with a Retrieve message.
For example:
//Create the complaint
Entity complaint = new Entity("incident");
//This is the information that is being sent to the server,
//it will not be updated by CRM with any additional information post creation
complaint["description"] = description;
complaint["ts_case_type"] = 717750001;
complaint["ts_registration_datetime"] = DateTime.Now;
complaint["ownerid"] = Owner;
complaint["customerid"] = Organisation;
//Capture the id of the complaint, we will need this in a moment
Guid complaintId = Service.Create(complaint);
//complaint["ticketnumber"] <-- The server does not populate this information in your object
//Retrieve the ticketnumber from the incident we just created
Entity complaintRetrieved = service.Retrieve("incident", complaintId, new ColumnSet("ticketnumber"));
//Get the ticketnumber
String ticketNumber = (String)complaintRetrieved.Attributes["ticketnumber"];
Like James said in comment, if you just want to send email with some case properties, it is best to do that with workflow (on case create).
In your plugin, ID is generated, and you can get it with:
entity.Attributes["ticketnumber"]

How to Get the Title of a SharePoint List?

I am trying to retrieve a list from SharePoint using the web services. I ran into the problem described in this blog post, i.e. the GetList method apparently expects to be passed the list's title instead of the list's name (even though the parameter is called "listName"). I have the list's name, but I cannot figure out how to get the list's title. Where can I find that?
I'm using the SharePoint in Office 365, which I believe is 2010.
A little over head but try this code. Its just a sample code, you might want to mould it to your logic.
string listName = "MyList";
Lists.Lists listSvc = new Lists.Lists();
listSvc.UseDefaultCredentials = true;
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(listSvc.GetListCollection().OuterXml);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("A", "http://schemas.microsoft.com/sharepoint/soap/");
XmlNode requiredList = xDoc.SelectSingleNode("//A:List[contains(#DefaultViewUrl,'" + listName + "')]", nsmgr);
string listTitle = requiredList.Attributes["Title"].Value;
XmlNode list = listSvc.GetList(listTitle);
strListName: Can be either a list name, such as "Documents", or a GUID of the list, with or without curly braces, in the following format:
{318B9E8F-1EF4-4D49-9773-1BD2976772B6}
you may find more info here - the above info is an excerpt from this document

SharePoint 2010 BaseFieldControl Duplicates first item in list when in DisplayMode

I have come across a problem when using the BaseFieldControl that is driving me to distraction.
Essentinally I am trying to convert a list into a HTML table using the BaseFieldControl to render the fields.
When my table renders it writes out the correct number of lines BUT the data in each line is always the same as the first item in the list.
When I change the ControlMode property from SPControlMode.Display to SPControlMode.Edit the list renders correctly ( apart from being in Edit mode )
When my code running with ControlMode set to SPControlMode.Display I can actually get at the correct value in the BaseFieldControl.ItemFieldValue property but the wretched BaseFieldControl still insists on rendering the first item in the list!
I've also installed the web part on a SharePoint foundation and SharePoint 2010 server and I get the same results!
Finally I've googled around and found other peoples examples. Unfortunately when I try other dev's code ( unmodified ) I get exactly the same results!
This is what I'm doing. Any suggestions would be really appreciated!
foreach (string f in list.DefaultView.ViewFields)
{
TableCell c = new TableCell();
var i = item[f];
if (i != null)
{
SPField spf = item.Fields.GetField(f);
BaseFieldControl bfc = spf.FieldRenderingControl;
bfc.ControlMode = SPControlMode.Display;
bfc.Value = bfc.ItemFieldValue;
bfc.ID = Guid.NewGuid().ToString();
bfc.FieldName = spf.Title;
bfc.ListId = list.ID;
bfc.ItemId = item.ID;
SPContext context = SPContext.GetContext(this.Context, item.ID, list.ID, SPContext.Current.Web);
bfc.ItemContext = context;
bfc.RenderContext = context;
bfc.EnableViewState = true;
bfc.Visible = true;
c.Controls.Add(bfc);
}
else
{
c.Text = "NULL";
}
r.Cells.Add(c);
}
I finally fixed it. Turns out to be a problem with the SPWeb object. I had grabbed it from SPContext and passed it through as a reference to my method.
When I stopped doing that but instead created it within the method ( and created it once per item in the list ) it all worked fine.
Very strange.

Enterprise Keyword not updating in SharePoint 2010

Any idea how to inject values to the Enterprise Keywords column of a List / Doc Lib Item using code?
Tried the following, it didn't give any error, but that column wouldn't update, while the Title did.
using (var site = new SPSite("http://testweb"))
{
using (var web = site.OpenWeb("testsite1"))
{
var list = web.Lists["testlist1"];
var item = list.AddItem();
item["Title"] = string.Format("Injected from code on {0}", DateTime.Now.ToString());
item["Enterprise Keywords"] = "1;#Opera|4eed0518-9676-4afc-be20-9027b3b69e42";
item.Update();
}
}
In this code, Opera keyword has been added previously, I've checked it against the TaxonomyHiddenList list as well using code to extract the correct ID and IdForTerm (the GUID).
What am I missing here?
To add a taxonomy field value the approach is a little bit different. Please try:
TaxonomyField entKeyword = (TaxonomyField)item.Fields["Enterprise Keywords"];
TaxonomyFieldValue term = new TaxonomyFieldValue("1;#Opera|4eed0518-9676-4afc-be20-9027b3b69e42");
entKeyword.SetFieldValue(item,term);
in stead of:
item["Enterprise Keywords"] = "1;#Opera|4eed0518-9676-4afc-be20-9027b3b69e42";

Resources