I have a Web Content in Liferay 6.2 and I want to add many versions to it, for test purposes.
How to do that without clicking thousands of times?
Go to the script console (in Server Administration), set it to "Groovy", paste the script below, replace the userId, groupId, articleId values with the ones found using the Liferay web interface, replace the numberOfVersions value with any number you want, then execute:
import com.liferay.portal.service.ServiceContext
import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil
import com.liferay.portlet.documentlibrary.model.DLFolderConstants
int numberOfVersions=1000
long companyId=20155
long groupId=21328
String articleId="21333"
long userId=20199
long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID
String content='<?xml version="1.0"?><root available-locales="en_US" default-locale="en_US"><static-content language-id="en_US"><![CDATA[Bonjour]]></static-content></root>'
ServiceContext serviceContext = new com.liferay.portal.service.ServiceContext()
serviceContext.setAddCommunityPermissions(true);
serviceContext.setAddGuestPermissions(true);
serviceContext.setScopeGroupId(groupId);
serviceContext.setCompanyId(companyId);
serviceContext.setUserId(userId);
for (int i=10; i<numberOfVersions; i++) {
double version = i/10.0
JournalArticleLocalServiceUtil.updateArticle(userId, groupId, folderId, articleId, version, content, serviceContext)
}
Any improvement or other idea is welcome!
Related
The requirement is as follows,
When a new web content(corresponding to a particular structure, say A) is published, it should automatically get updated on the Asset Publisher portlet (default functionality of Asset Publisher).
By default the Title of the web content is what appears as a link on the Asset Publisher for different web contents.
Instead of this I want the content of an element (say name) of structure A to appear as a link. Clicking on this link should open an Alloy UI Popup containing the corresponding Web content.
For this to happen I created a new 'display style' jsp using hooks (tweaked the abstracts.jsp).
Wrote this scriptlet in the .jsp:
<%
String personName=null;
JournalArticle journalArticle=null;
String myContent=null;
Document document = null;
Node node=null;
Node node1=null;
Node node2=null;
Node node3=null;
int noOfWords=0;
String pic=null;
String aboutMe=null;
double version=0;
try {
version=JournalArticleLocalServiceUtil.getLatestVersion(assetRenderer.getGroupId(), "14405");
journalArticle = JournalArticleLocalServiceUtil.getArticle(assetRenderer.getGroupId() , "14405",version);
myContent = journalArticle.getContent();
document = SAXReaderUtil.read(new StringReader(myContent));
node = document.selectSingleNode("/root/dynamic-element[#name='personName']/dynamic-content");
if (node.getText().length() > 0) {
personName = node.getText();
}
node1 = document.selectSingleNode("/root/dynamic-element[#name='pic']/dynamic-content");
if (node1.getText().length() > 0) {
pic = node1.getText();
}
node2 = document.selectSingleNode("/root/dynamic-element[#name='noOfWords']/dynamic-content");
if (node2.getText().length() > 0) {
noOfWords = Integer.parseInt(node2.getText());
}
node3 = document.selectSingleNode("/root/dynamic-element[#name='aboutMe']/dynamic-content");
if (node3.getText().length() > 0) {
aboutMe = node3.getText(). substring(0,noOfWords)+"....";
}
} catch (PortalException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
%>
But here the articleId needs to be hard coded.
I want to fetch the articleId here as and when a new web content is published i.e. dynamically.
Which API should be used here?
Any help is appreciated.
Thanks.
This method works for me on the latest version of Liferay - Liferay 6.1.1 CE GA2, but I think it should works without any changes on previous versions too.
Briefly, you could use getClassPK() method of the AssetEntry instance.
In all of the display jsps you get asset entry as request attribute:
AssetEntry assetEntry = (AssetEntry)request.getAttribute("view.jsp-assetEntry");
And then to get latest version of journal article that's associated with asset entry instead of using:
double version =
JournalArticleLocalServiceUtil.getLatestVersion(assetRenderer.getGroupId(),
articleId);
JournalArticle journalArticle =
JournalArticleLocalServiceUtil.getArticle(assetRenderer.getGroupId(),
articleId, version);
you could just write:
JournalArticle journalArticle =
JournalArticleLocalServiceUtil.getLatestArticle(assetEntry.getClassPK());
Hope this helps.
I am working on SharePoint 2010.I have an documentlibrary ID and document ID in that library with me.i don't have either web,site in which the document library is present.So now I have to get the Full URL of the document at runtime.How can I get it .
I have tried the following.
string filepath = currentList.DefaultViewUrl + "/" + sListItem.Url;
Please answer this.
Use the field "EncodedAbsUrl" on the SPListItem. Works for SPFile as well:
SPListItem item = ...;
string absUrl = (string) item[SPBuiltInFieldId.EncodedAbsUrl];
or for a SPFile
SPFile file = ...;
string absUrl = (string) file.Item[SPBuiltInFieldId.EncodedAbsUrl];
The built in field id is for sure the best way to go but it returns the Url as encoded which may or may not be what you want.
I think the best way is to add a little extension method to a utilities class somewhere:
public static string AbsoluteUrl(this SPFile File, bool Decode = true)
{
string EncodedUrl = File.Item[SPBuiltInFieldId.EncodedAbsUrl].ToString();
if (Decode)
return SPEncode.UrlDecodeAsUrl(EncodedUrl);
else
return EncodedUrl;
}
And then call as follows
Item.File.AbsoluteUrl();
if you want a decoded Url or
Item.File.AbsoluteUrl(false);
if you want the Url to remain encoded.
Note that the default parameter value for Decode will only be available in .Net4+ and therefore SP2013 only but you can easily create an overload method for SP2010. You'll also need a reference to Microsoft.SharePoint.Utilities namespace to access the SPEncode class.
Try this ,
using (SPSite ospSite = new SPSite("http://abcd:24931"))
{
using (SPWeb web = ospSite.OpenWeb("/subsite")
{
// Get document library collection here and fetch all the document urls
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["docu"];
//where docu is my document library
SPListItemCollection items = docLib.Items;
foreach (SPListItem item in items)
{
string url = item.Url;
}
}
}
Hope this shall get you going.
public string GetItemURL(SPListItem item)
{
string web = item.Web.Url;
string listID = item.ParentList.ID.ToString();
string contentType = item.ContentTypeId.ToString();
string itemID = item.ID.ToString();
string url = web+"/_layouts/listform.aspx?PageType=4&ListID={"+listID+"}&ID="+itemID+"&ContentTypeID="+contentType;
return url;
}
It's Working for me. Hope I help (List Item url)
If this is for the document library, try this one.
item.Web.Url+"/"+item.File.Url
Use below code to get absolute URL of file:
SPFile file;
string url = file.Item[SPBuiltInFieldId.EncodedAbsUrl] as string;
For what it's worth, accessing the item.Web property means you are actually instantiating the SPWeb object which means that this should be disposed otherwise you'll be causing memory leakage.
It's a lot of overhead when there are better and quicker ways already mentioned.
I would use the BuiltInFieldId.EncodedAbsUrl approach mentioned since this gives you the easiest access to what you want.
The answer is
string url = currentweb.url+"/"+ Listitem.url;
From code I've automatically created a lot of similar sites (SPWeb) in my site collection from a site template (in Sharepoint Foundation). Every site has a home page on which I've added the "what's new" web part (found under "Social collaboration").
Even though the web part has several "target lists" (I'd have called it "source lists") added to it on the template site, this connection is lost on the sites created from the template. So I need to programmatically find all these web parts and add the target lists to them. Looping the web parts is not an issue - I've done that before - but I can't seem to find a word on the net on how to go about modifying this particular web part. All I have is a brief intellisense.
I've found out that it recides in the
Microsoft.SharePoint.Applications.GroupBoard.WebPartPages
namespace, but on the lists provided on MSDN this is one of very few namespaces that doesn't have a link to a reference documentation.
Does anyone have any experience of modifying this web part from code? If not, how would you go about to find out? I can't seem to figure out a method for this..
Here is how I did it. It worked really well. I had a feature that created several list instances and provisioned the What's New web part. In the Feature Receiver, I looped through all of the list instances, indexed the Modified field, and then added the list to the web part:
private void ConfigureLists(SPWeb web, SPFeatureReceiverProperties properties)
{
List<Guid> ids = new List<Guid>();
SPElementDefinitionCollection elements =
properties.Feature.Definition.GetElementDefinitions(new CultureInfo((int)web.Language, false));
foreach (SPElementDefinition element in elements)
{
if ("ListInstance" == element.ElementType)
{
XmlNode node = element.XmlDefinition;
SPList list = web.Lists[node.Attributes["Title"].Value];
SPField field = list.Fields[SPBuiltInFieldId.Modified];
if (!field.Indexed)
{
field.Indexed = true;
field.Update();
}
ids.Add(list.ID);
}
}
string targetConfig = string.Empty;
foreach (Guid id in ids)
{
targetConfig += string.Format("'{0}',''\n", id);
}
SPFile file = web.GetFile("Pages/default.aspx");
file.CheckOut();
using (SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
WhatsNewWebPart webpart = null;
foreach (System.Web.UI.WebControls.WebParts.WebPart eachWebPart in manager.WebParts)
{
webpart = eachWebPart as WhatsNewWebPart;
if (null != webpart)
{
break;
}
}
if (null != webpart)
{
webpart.TargetConfig = targetConfig;
manager.SaveChanges(webpart);
}
}
file.CheckIn("ConfigureWebParts");
file.Publish("ConfigureWebParts");
file.Approve("ConfigureWebParts");
}
If you are unsure about the property, export the web part from the browser, then open the .webpart/.dwp file with a text editor. Somewhere in the xml will be a reference to the source list.
*.webparts are usually easier to modify, just set the property.
*.dwps are harder because you sometimes have to get the property (eg ViewXML), then load it into an XmlDocument, then replace the property, and write the xml document string value back to ViewXML.
Is it possible to add a portlet programmatically? If yes, please help me to understand the steps for that.
Liferay CE comes with the "sevencogs-hook" that contains code to set up the demo content, e.g. the "Seven Cogs" virtual company used to demo a Liferay site. This sets up a complete site programmatically. You can read that code and learn how users and pages are created, portlets are added to pages and configured to show what they are supposed to show. More than that: It's running code that can easily be read and is - as side effect - a nice piece of documentation.
http://svn.liferay.com/repos/public/plugins/trunk/hooks/sevencogs-hook/
Login: "guest", no password
or download the source for the version you're referring to.
Something like:
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
Layout layout = themeDisplay.getLayout();
long plid = layout.getPlid();
long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
long companyId = themeDisplay.getCompanyId();
String portletIdInc = layoutTypePortlet.addPortletId(userId, thisPortletID);
// Retrieve the portlet preferences portlet instance just created
PortletPreferences prefs = PortletPreferencesLocalServiceUtil
.getPreferences(companyId, ownerId, ownerType, plid, portletIdInc);
// set desired language
String languageId = LanguageUtil.getLanguageId(request);
String urlImage = .... ;
prefs.setValue("portlet-setup-title-" + languageId, report.getName());
prefs.setValue("portlet-setup-use-custom-title", "true");
prefs.setValue("src", report.getUrl());
prefs.setValue("img", urlImage);
prefs.store();
String targetColumn = "column-1";
// update the portlet preferences
PortletPreferencesLocalServiceUtil.updatePreferences(ownerId,
ownerType, plid, portletIdInc, prefs);
if (Validator.isNotNull(targetColumn) && Validator.isNotNull(portletIdInc)) {
layoutTypePortlet.movePortletId(userId, portletIdInc, targetColumn, 2);
}
LayoutServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(),
layout.getLayoutId(), layout.getTypeSettings());
We're running SharePoint 2007 SP1 and profiles are imported from Active Directory (a full import runs daily). We had a problem where many of the users were disabled unintentionally in Active Directory and this caused their profiles to be removed from SharePoint. We re-enabled their Active Directory accounts and ran a full import which restored their SharePoint profiles. However, all of their My Links are missing. Is there a method or best practice for restoring them?
I posted this because I couldn't find an answer to my problem anywhere. This post by Joel Oleson that describes a similar problem to mine gave me a hint as to where to go looking for the missing data. And This post by Corey Roth showed me how to programatically add the links to a users My Links.
First things first - you need to restore a backup of the database that contains the My Links data. You don't want to restore over your working database, you want to restore it to another location. The links stored in the SSP Database. (You can find out the name of the database by going into Central Admin --> Shared Services Admin then open the menu for the SSP and click on Edit Properties - the SSP Database is listed on the properties page.)
Once the database has been restored you want to retrieve the Link information:
the domain account name of the user who owns the link,
the url of the link
the name of the link
This query will get you that information:
SELECT UPF.NTName, UL.Url, UL.Title
FROM UserLinks UL INNER JOIN UserProfile_full UPF ON UL.recordID = UPF.recordID
INNER JOIN UserPrivacyPolicy UPP ON UL.PolicyId = UPP.id
ORDER BY NTName
(I should note that I did not take into account what group or what privacy level the links were set to, you could probably find that information by looking at the information in the UserPrivacyPolicy table)
I copied the results into Excel & saved it as a .csv file (comma separated list) - just because my production server did not have access to the location where I restored my database. I ordered the columns with Title last because the Title could contain commas and that would mess up how I'm reading in the data. (I checked and the other two fields do not contain commas - you should check yours before making this assumption.)
I then wrote a little console app to import the data. It takes two arguments:
the path where the file containing all of the links is located (ie c:\temp\links.csv)
the url of the SSP from with the My Links have gone missing (ie https://portal.mydomain.com)
These are the references used:
Microsoft.Office.Server (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.Office.Server.dll)
Microsoft.SharePoint (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll)
System
System.Data
System.Web
System.Xml
And this is the code:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Web;
namespace UserLinks
{
class Program
{
static void Main(string[] args)
{
string _accountName = "", _linkTitle = "", _url = "", _tmp = "", _path = "", _SPSsite = "";
// Check arguments
if (args.Length != 2)
{
ShowUsage();
return;
}
_path = args[0];
_SPSsite = args[1];
using (SPSite _site = new SPSite(_SPSsite))
{
ServerContext _context = ServerContext.GetContext(_site);
UserProfileManager _userProfileManger = new UserProfileManager(_context);
/* Expecting a comma seperated list with 3 columns:
* AccountName in the format Domain\Account name - I am assuming there are no commas in this field
* URL - I am assuming there are no commas in this field
* Link Title - link title is last because there may be commas in the title
*/
TextReader _reader = new StreamReader(_path, System.Text.Encoding.Default);
while (_reader.Peek() != -1)
{
_tmp = _reader.ReadLine();
_accountName = _tmp.Substring(0, _tmp.IndexOf(','));
_tmp = _tmp.Replace(_accountName + ",", "");
_url = _tmp.Substring(0, _tmp.IndexOf(','));
_linkTitle = _tmp.Replace(_url + ",", "");
try
{
UserProfile _currentUser = _userProfileManger.GetUserProfile(_accountName);
QuickLinkManager _quickLinkManager = _currentUser.QuickLinks;
_quickLinkManager.Create(_linkTitle, _url, QuickLinkGroupType.General, null, Privacy.Private); //I am assuming that all links have no group assigned to them and they are all private links
}
catch (Exception ex)
{
Console.WriteLine(_accountName);
Console.WriteLine(ex);
}
}
_reader.Close();
}
}
private static void ShowUsage()
{
Console.WriteLine("Usage");
Console.WriteLine("UserLinks [FilePath] [SharePoint URL]");
}
}
}
So problem solved & as a side benefit, this program can be used to force links to show up in a user's My Links list.
This post has some pretty good information about MyLinks and its relationship with the SSP database (that's actually where these links are stored counterintuitively.) Hopefully you can have your DBA validate that these links still exist; and that they're associated with the correct profiles.
http://www.k2distillery.com/2009/01/moving-sharepoint-my-links-between-ssps.html
When you do a profile import, you normally risk losing the existing customization/updated information.