Copy whole WCM library using WCM API

To copy the whole library follow below two steps

1. create JSP file called it as "copyLibrary.jsp" , this will take input name of what library need to copy
2. create anothoer JSP and call it as "copyLibraryContent.jsp" , it does the actual copy job


1. Code for copyLibrary.jsp as follows
<form action="copyLibraryContent.jsp" method="POST">
Enter Library Name : <input type="text" name="libName" size="20" value="">
<input type="submit" name="Copy" value="Copy"><br>
</form>



Enter Library Name :




2. Code for copyLibraryContent.jsp as follows

<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" session="false"%>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%@page import="java.security.*"%>
<%@page import="javax.naming.*"%>

<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="com.ibm.workplace.wcm.api.exceptions.*"%>
<%@ page import="javax.servlet.jsp.JspWriter"%>
<%@ page import="javax.servlet.http.HttpServletRequest"%>
<%
String libName = (String)request.getAttribute("libName");
if(null == libName||libName.trim().length() <1)libName =(String)request.getParameter("libName");
if(null == libName||libName.trim().length() <1){
out.println("<br/><br/><br/><br/><h2><font color='red'><b>Library doesnt exists, Enter valid Library name</b></font></h2><br/><br/>Clck here to go <a href='copyLibrary.jsp'><b>back</b></a>");return;
}

out.println("<br/><br/><br/><br/><h4><font color='black'><b>Copying Library....."+libName+"</b></font></h4><br/>");
try
{
// Construct and inital Context
InitialContext ctx = new InitialContext();

// Retrieve WebContentService and WebContentLibraryService using JNDI name
WebContentService webContentService = (WebContentService) ctx.lookup("portal:service/wcm/WebContentService");
WebContentLibraryService webContentLibraryService = (WebContentLibraryService) ctx.lookup("portal:service/wcm/WebContentLibraryService");

Workspace ws = webContentService.getRepository().getWorkspace("wpsadmin", "test123");
//DocumentLibrary[] docLib = {ws.getDocumentLibrary("Library Name")};
DocumentLibrary[] docLib = {ws.getDocumentLibrary(libName)};
if(null == docLib[0]){out.println("<br/><br/><br/><br/><h2><font color='red'><b>Library doesnt exists, Enter valid Library name</b></font></h2><br/><br/>Clck here to go <a href='copyLibrary.jsp'><b>back</b></a>");return;}
LibraryTaskResult res = webContentLibraryService.copyLibraries(ws, docLib);

// Once you get the result object back, print status to StandardOut
if (res.getResultType() == ResultTypes.OPERATION_SUCCESS)
{
System.out.println("Successfully Copyied Library " + res.getDocumentLibraries()[0].getTitle());
out.println("Successfully Copyied Library " + res.getDocumentLibraries()[0].getTitle()+"</b></font></h4><br/><br/>Clck here to <a href='javascript:window.close();'><b>close</b></a>");
webContentLibraryService.saveLibrary(ws,res.getDocumentLibraries()[0]);
}
else
{
System.out.println("Failed To Copy Library ");
out.println("<br/><br/><br/><br/><h2><font color='red'><b>Failed To Copy Library </b></font></h2><br/><br/>Clck here to go <a href='copyLibrary.jsp'><b>back</b></a>");
}
}
catch (Exception e)
{
e.printStackTrace();
}

%>

1 comment:

  1. Good article, thanks.
    I need to copy some site areas to root area (library)via api. Workspace object has a copy method with ParentLocation(DocumentId) parameter, how I can pass library as this parameters, library doesn't have documentId.

    ReplyDelete