Skip to Content

*Follow SAP RIG online: *Twitter | Facebook

Greetings,

During the project involvement with NetWeaver BPM, one of the frequently asked questions is “how to use UWL api for BPM Tasks?”. A good example is the customer wants to generate the “direct URL link” to the specific BPM task. In this way we can put the link in email and the end user doesn’t always have to go to the UWL list and open task item. The UWL API is well documented in the form of Javadoc (http://help.sap.com/javadocs/nwce/current/uwl/com.sap.uwl/index.html), but  the sample code is missing.

I had a talk with the UWL development team and they shared a good sample code. You can steal the source to avoid the double effort ๐Ÿ™‚  (*Disclaimer: *SAP does not warrant the correctness and completeness of the Code given herein, and SAP shall not be liable for errors or damages caused by the usage of the Code.)In order to use the package “import com.sap.netweaver.bc.uwl.*”, you need to find the Software Component “UWLJWF.sca” at the service marketplace. You can go search and find it via “Support Packages and Patches – Entry by Application Group” > “SAP NetWeaver” > “SAP NETWEAVER” > “SAP EHP1 FOR SAP NW CE 7.1” – “Eintrag nach Komponente” > “BPM Portal Integration”  and you will find “UWL COLL PROCESS ENGINE 7.11” in the list.

Here you will find the right Software Component “UWLJWF.sca”. You need to import it in your LocalDevelopment of NWDS in the Development Infrastructure perspective.

After the import, define the dependency for MyComponent with UWLJWF.

In this way your DC can define the dependency of the UWL API. Go to your DC and define the dependency. You only need to choose “*tc/kmc/bc.uwl/api*” under UWLJWF.* *

* *

That’s it!* *

 

———– Sample begins———- 

package samplepackage;

import java.util.Iterator;

import java.util.Locale;

import java.util.Properties;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import com.sap.netweaver.bc.uwl.IUWLItemManager;

import com.sap.netweaver.bc.uwl.IUWLService;

import com.sap.netweaver.bc.uwl.IUWLSession;

import com.sap.netweaver.bc.uwl.Item;

import com.sap.netweaver.bc.uwl.ItemCollection;

import com.sap.netweaver.bc.uwl.QueryResult;

import com.sap.netweaver.bc.uwl.UWLContext;

import com.sap.netweaver.bc.uwl.UWLException;

import com.sap.security.api.IUser;

import com.sap.security.api.UMFactory;

public class Sample {

  public void retriveItems() {

 

    try {

            // look up UWL service

            IUWLService uwlService = findService();

            // define session timeout period

            final int sessionIdleTimeout = 60;

            // create context

            UWLContext uwlContext = new UWLContext();

            // find logged in user.

            IUser user = //TODO Get the user for whom the items are to be retrieved.

            uwlContext.setUser(user);

            uwlContext.setLocale(Locale.getDefault());

            // begin session IUWLSession uwlSession;

            uwlSession = uwlService.beginSession(uwlContext, sessionIdleTimeout);

            uwlContext.setSession(uwlSession);

            IUWLItemManager itemManager = uwlService.getItemManager(uwlContext);

            QueryResult result = itemManager.getItems(uwlContext, null, null);

            ItemCollection items = result.getItems();

             Item item = null;

            for (int i = 0; i < items.size(); i++) {

              item = items.get(i);

              // item.getSubject() can give you Task’s Subject string

              // item.getAttribute(“taskId”).getStringValue() gives you taskId

              // (a check for null should be added also to avoid NullPointerException).

              Map params = new HashMap();

              params.put(“taskId”, item.getExternalId());

              String executionURL = WDURLGenerator.getApplicationURL(“sap.com/tc~bpem~wdui~taskinstance”, “ATaskExecution”, params);

              //TODO Add code send mail which contains above URL.

            }

    } catch (UWLException e) {

            e.printStackTrace();

          } catch (NamingException e) {

            e.printStackTrace();

          }

  }

 

  // look up UWL service private

  IUWLService findService() throws NamingException {

    Properties env = new Properties();

          env.put(InitialContext.INITIAL_CONTEXT_FACTORY, “com.sapportals.portal.prt.registry.PortalRegistryFactory”);

          // create initial context

          InitialContext ctx = new InitialContext(env);

          // retrieve UWL service

          IUWLService uwlService = (IUWLService) ctx.lookup(“/broker/services/” + IUWLService.ALIAS_KEY); return uwlService;

  }

}


– ———- Sample ends—- ——

To report this post you need to login first.

49 Comments

You must be Logged on to comment or reply to a post.

  1. Prasad Nutalapati
    Hello:
    My customer is asking exactly the same thing.
    I thought like “who would like the url of the task
    to be sent in an email”, but they do.
    The people at the customer don’t want to or don’t
    know how to go to the UWL, for them this is just
    right.

    Thanks for Just In Time knowledge,

    Prasad Nutalapati

    (0) 
        1. L. Vandousselaere
          Hi,

          I just noticed I’m not receiving all my tasks, only (some of) the completed ones from the past.

          Do you have any idea why this is? I’ve been looking for answers online, but there isn’t much documentation about uwl …

          Thanks in advance, Lynne

          (0) 
  2. Vic Chen
    thank you, it work.
    i use it in my web dynpro project, but it doesn’t work well in caf.
    i tried get current iUser to get items in caf application service with
    UMFactory.getUserFactory().getUserByLogonID(userId);
    i get exception: com.sap.netweaver.bc.uwl.UWLException: Logged in users context or session doesn’t exist.
    i have no idea why it work with web dynpro but caf.
    can you tell me why? thank you very much.
    (0) 
    1. Kenichi Unnai Post author
      Hi Vic,

      It should work. Most likely you have some issues…I suggest to take a closer look:

      uwlContext.setUser(user);
      uwlContext.setLocale(Locale.getDefault());

      and

      uwlSession = uwlService.beginSession(uwlContext, sessionIdleTimeout);
      uwlContext.setSession(uwlSession);

      (0) 
      1. Vic Chen
        Thank you very much, Mr.

        i guess i found the root cause of the issue.
        because the security reason, it will not work when the iUser ain’t authentic.
        when i use it in web dynpro project, i use such like follows

        IWDClientUser wdUser = WDClientUser.getCurrentUser();
        IUser user = wdUser.getSAPUser();

        it work.

        but i need not only get my task info., but also get other user’s task info.
        did you tried it in caf and work??

        anyway, good day and thank you for your fast reply.
        thank you.

        (0) 
        1. Kenichi Unnai Post author
          Hi Vic,

          You might want to post this detailed question to the forum, would be useful if you put the URL link here as well. I haven’t tried the caf yet but the development colleagues said it should work in caf too.

          Best regards,
          Ken

          (0) 
        2. David Palafox
          Hi, I hope you guys get this message, I see this blog has been around for quite a while.

          I have a requirement in which, given a role, we want to find out which user in that role has the least items in their UWL so we can then assign a new task to that user. This would ideally be done via a web service using a lot of what was discussed in this blog. I posted a couple questions on the forums, and haven’t got an answer yet. I’m hoping you can point me in the right direction to get this done or at least let me know that it can’t be done with the current API.

          Here’s my questions…

          Getting tasks for all users in a role

          Getting UWL task count for all users in a role.

          I also tried doing the same thing, but instead doing it with a WS, doing it with a WD application, basically to get proof of concept but haven’t been able to get it to work either.

          Here’s my test scenario:

          role: testRole
          users: trUser1 to trUser5

          Let’s say I run the application which needs to be authenticated, and log in using trUser1. If I set the UWLContext to be the same as the user I log in with, it actually works and I am able to see the number of tasks.

          so if I…

          IUser contextUser = UMFactory.getUserFactory().getUser( “USER.PRIVATE_DATASOURCE.un:trUser1” );
          context.setUser  ( contextUser );

          …and then log in to the application with trUser1, it works. but if I set any other user for context user, it doesn’t.

          Problem is that I need all users in a role, so i use an array to hold the userIDs and then loop through it, and in the end, whichever user I log in with, will show the number of tasks, and the others show a zero.

          Again, this is not what we actually want, I tried the WD application to try and prove that it could be done, I haven’t been able to do so yet, and ideally, we will be invoking a WS that will return the one user with the least tasks.

          Thanks so much for taking the time to read this. Hope you can give me some desperately needed help.

          (0) 
          1. Kenichi Unnai Post author
            Hi David,

            Sorry it took a while to discuss with the UWL & UME colleagues to clarify.

            The answer is unfortunately you can’t swith the user at runtime, as this raises the potential security risk.

            Alternatively you could reassign the role with following code:

            IUser iuser = request.getUser();
            IRole irole = UMFactory.getRoleFactory().getRoleByUniqueName(“YOUR_NEW_ROLE”);
            if (!irole.isUserMember(iuser.getUniqueID(),false)){
                irole = UMFactory.getRoleFactory().getMutableRole(irole.getUniqueID());
                irole.addUserMember(iuser.getUniqueID());
                irole.save();
                irole.commit();
            }

            I am not 100% sure if this works with UWL, but you can try…

            (0) 
            1. David Palafox
              Thank you so much for responding… Truth be told, I think I do get what your code does, but I don’t see how I could use that. If you think that works for me, I’d appreciate it if you could further explain the usage of your code. Anyway, just for clarification, here’s what I need:

              In our process, a user in roleA fills out an application which is then checked by another user, member of roleB. We know how to assign a task to a user at runtime, and we’d like to do so by using an expression which gets the user in roleB (or any role for that matter, the function would ideally receive a role name as a parameter) with the least amount of tasks in their UWL, so that the task will be assigned to them. Code is EJB to be placed in EJB container and added to the Rules and Functions of the BPM process, and invoked in the “Use an Expression” option under Potential Owners under Roles of the task itself.

              I’s sorry if I’m being somewhat overexplicative, but I’m really starting to believe this can’t be done and I’d really like to know if what we want can be done. Hope this makes it all clearer.

              Thanks again,
              David…

              (0) 
              1. Kenichi Unnai Post author
                Hi David,

                I assume you’re trying to implement a very smart task dispatcher program by which it can assign the dynamic user(s) for the right incoming task.

                I just went through what you had posted in the forums and as we have the fact that UWL API doesn’t allow you to switch the user ID assignment for security reason, I have a feeling that what you’re trying to do is not really feasible.

                (0) 
  3. Martin Bรคuml
    Hi,
    thank you for this information. That’s exactly I was looking for. Due to the fact that I am new in this whole topic I do not know where I can build in this code. 

    May you explain or send me a project which I can deploy an my CE?

    Regards

    (0) 
    1. Kenichi Unnai Post author
      Hi Martin,

      Unfortunately I don’t have the complete out-of-the box sample apps right now, but the easiest way to try running this code should be with Web Dynpro Java app. Simply create your WDJ app in NWDeveloper Studio IDE and invoke the
      retriveItems() method (the code in my blog) in the wdDoInit() method.

      Hope this info helps you ๐Ÿ™‚ 

      (0) 
      1. Martin Bรคuml
        Hi, thank you very much for this fast response. First I was a little bit inert because I have nearly no experience in WebDynpro, but in the end I got this example running – yeah ๐Ÿ˜€

        May it would be possible to give a hint in your blog to
        1. Download the UWLJWF04
        2. Follow this tutorial http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/802bfda2-8a85-2b10-9290-d67da32d0c01 by using the previous downloaded .sca
        3. Replace the tutorial code of the getInbox() method with yours

        Regards.

        (0) 
        1. Kenichi Unnai Post author
          Thanks – your post makes the blog even more informative & useful. I am sure all the colleagues who are interested in this topic will appreciate your contribution.
          (0) 
  4. Paulo Jorge Ruivo
    Hi,
    thanks for the great code sample.

    I would like to implement also the forward action in BPM items using the UWL API.
    For this I tried to use this code:
      Map params = new HashMap();
    params.put(“userIdList”, “USER.PRIVATE_DATASOURCE.un:XXXX”);
      action = itemManager.getActionForItem(uwlContext, item, Action.ACTION_FORWARD);
    ProviderStatus status = itemManager.performAction(uwlContext,item, action, params);

    In status variable I get a sucessful message, but items are not forward, they stay in the same user.

    Is this a limitation in BPM items, or is something wrong in this code?

    Thanks,
    Paulo Ruivo

    (0) 
    1. Kenichi Unnai Post author
      Hi Paulo, sorry for the very late reply, I have been discussing about this with dev. team for a while…

      Unfortunately it turned out this Foward action is not supported as of 720.

      There’s some ongoing discussion to explore the possibility to achieve this through public BPM API on 730 (not through UWL API).

      Anyway thanks for the great feedback. This does contribute the product improvement.

      (0) 
  5. san zhang
    Hello,

    I have checked the blog and code sample.

    We can use these codes to get bpm task itsms,But we can not get WORK ITEMS FROM ERP SYSTEM even 3 or more hours later after creating some tasks.

    Could you help me?

    (0) 
      1. san zhang
        Hello Kenichi,

        Thanks for your replay. We do have configured the uwl iview and we can get all types work items by logging into CE and going to uwl iview page. 

        The problems is that our client asks us to get task work itsm info and show them(SAP or NO-SAP)in one table.

        we have tried and found we can not get erp work item data by uwl api until you enter into the uwl iview page.

        (0) 
    1. Kenichi Unnai Post author
      Namaste Bharat ๐Ÿ™‚

      Unfortunately until 720, there’s no public BPM API. You can come up with some workaround by means of custom EJBs or web services, but in some case it is simply not possible (ex. grabbing process id).

      We can expect some public API on 730…

      (0) 
  6. Craig Gutjahr
    Hello,

    Has anyone been able to figure out how to get around the issue of cache clearing when using the UWL API.

    Basically, if you clear the UWL cache, the API will be reset to 0. The only way to have the API display the correct number of items is to view the UWL.

    (0) 
  7. Yomesh Sharma
    Hello,

    Great blog !! helps a lot.

    I am facing a challenge though in getting the current uwl task id in my webDynpro application. I check in UWL configuration, on click of a BPM task, a sap application sap.com/tc~bpem~wdui~taskinstance is called and a WebDynpro app parameter taskid is passed to this application. I have a requirement, where I would like to know the current task id in wd application which is part if the BPM process when it is called.

    In your sample code you are looping through all tasks assigned to the user. Is there a way I can determine current task id.

    Thanks,
    Yomesh

    (0) 
    1. Kenichi Unnai Post author
      Hi Yomesh,

      Thanks for the post. I had a talk with UWL colleagues and you would need to need to clarify:

      >What does “current task” mean in this context?

      As you can grab the task id as shown in the code sample, I assume you need something else?

      (0) 
      1. Yomesh Sharma
        Hi,

        Thanks a lot for a follow up.

        Lets take an example where a User has 5 items in his UWL. Each of this 5 tasks have a unique task ID. If a user clicks on 1st task in UWL, I would like to launch a WebDynpro Application in which I can read the task ID of this 1st Task(this is current task ID in the context as it was clicked out of 5 items in the UWL).

        Hope I was able to explain what I am looking for.

        Thanks for your response,
        -Yomesh

        (0) 
        1. Revathi Raju
          Hi Yomesh, I have the same requirement that you have asked for. Were you able the solve the problem using Webdynpro launcher. If so, how to do. I understand that we need to pass externalid as a parameter to Webdynpro launcher. But how to identify the current task ID. Please guide me. Thanks.
          (0) 
          1. Yomesh Sharma

            Hi Raju,<br/><br/>Yes, the SAP WebDynpro Launcher worked for me. <br/><br/>1. Go to System Administration > System Configuration>Universal Worlist Administration.Yomesh

            (0) 
            1. Revathi Raju
              Hi Yomesh, Thanks a lot for your reply.

              I am able to open the custom webdynpro applications from UWL. How to get the current task id. In your code, String taskId = request.getParameter(“taskId”);, what is the parameter that you are passing as “taskId”. Please help. Thank you.

              (0) 
              1. Hiroyuki Otaki
                Hi,

                You can get task instance id by Yomesh’s code in appropriate position such as wdDoModifyView(). Here is a sample below.
                —-
                  public void wdDoModifyView(IWDView view, boolean firstTime)
                  {
                    if (firstTime) {
                      IWDProtocolAdapter adapter = WDProtocolAdapter.getProtocolAdapter();
                      IWDRequest req = adapter.getRequestObject();
                      String taskId = req.getParameter(“taskId”);
                      //Add your code here
                  }
                —-
                By the way, I could retrieve task instance id without any UWL configuration. This code should work on standard WebDynpro component.

                Enjoy,
                Hiro

                (0) 
    1. Kenichi Unnai Post author
      Shalom Asaf,

      Thanks for the posting. I had a talk with dev. colleagues and at this moment we can’t configure the display of Task Data Panel.

      (0) 
        1. Kenichi Unnai Post author
          Shalom Asaf,

          I had an update from other team than UWL that the on/off of the task panel is now available. You can follow the SAP Note#1456788. You might need to apply the attached patch to make all the params visible & configurable for you.

          (0) 
  8. Pavan Patil GC
    Hi Kenichi,

    i have tried to create simple java class for the solution you have suggested and ended up in getting exception “Exception in thread “main” java.lang.NoClassDefFoundError: EDU/oswego/cs/dl/util/concurrent/ConcurrentReaderHashMap” for the line
    InitialContext ctx = new InitialContext(env);

    and tried WD dc and portal component dc with following code
    user = UMFactory.getUserFactory().getUser(“USER.PRIVATE_DATASOURCE.un:User1”);

    and got exception “Wed May 04 10:05:31 GMT+05:30 2011 (Default) Logged in users context or session doesn’t exist” ,
    Without authenticating a user it wont fetch any task detais.
    With Following iuser (authenticated session) it works
    user = WDClientUser.getCurrentUser().getSAPUser();

    is there any way to get all the tasks of any user with the api (may be with some admin role/s)?

    (0) 
    1. Kenichi Unnai Post author
      Hi there, thanks for inquiry.

      >Without authenticating a user it wont fetch any task detais.

      Right, APIs are intentionally designed for it, due to the security protection. So you have to authenticate specific SAP users to access these info.

      (0) 
        1. Kenichi Unnai Post author
          Namaste Pavan,

          Sorry it’s been a while, I had been discussing with my fellow dev. folks about your question. There is a common confusion like you pointed out and javadoc of UWL API should be updated sometime soon. Here’s the comment:

          “the UWL api will only return the task for logged in user and not for any other user. It will not be ok to do so also simply from the point of view access permissions. Also the scenario where an admin could fetch tasks for all users for may be monitoring or some other use case is  not supported.”

          (0) 
          1. Swapnil Shelke
            Hi Mr. Unnai
            It is more informative blog. I need guidance to download and find the UWLJWF.sca or com.sap.netweaver.bc.uwl.jar file for CE7.1 application.
            In this application I am creating the new tab for UniversalWorklist, so please give me porper guidance…

            Thanks
            swapnil

            (0) 
  9. Tee Wei Eric Goh

    Hi Unnai,

    I’m currently on NW 2004s. For your solution, does it work on my old version of NW? And if it happened to be yes. Do all the steps you had mentioned to be similar? Please advice. You help is appreciated.

    Have a nice day.

    Best Regards,

    Eric

    (0) 
    1. Kenichi Unnai Post author

      Hi Eric,

      It should work if your NW system has BPM components, this UWL tip works with NW BPM.

      I don’t quite remember NW 2004s has BPM, I remember you would need NW CE? If you are looking for scenario where you don’t need BPM task, you can refer other resources on UWL in general.

      Hope this helps,

      Ken

      (0) 
  10. Alessandro donati

    Hello

    first of all

    congratulations
    for the article.

      

    I have some
    problems getting this library within my DC, or rather in the local dc I
    imported but

     

    Development
    in Remote Configuration not find uwdjwf even if it exists in dir “/ usr /
    sap / <SID> / SYS / EPS / buildarchives”
    “UWLJWF05_0-20002656.SCA” the same package is deployed in our dtr and
    I can see it there.

        

    if I try to
    put the webdynpro I have on remote Configuration Development as a dependency in
    dipendencies it does not appear among the dcs that can be referenced, there I
    have only:

        

    ENFASCADE

    ENGINEAPI

    ESCONF_BUILDT

    FRAMEWORK

    MOINT_BUILDT

    SAP_BUILDT

    WD-RUNTIME

    WSRM

    Can you
    help me?

    Greetings Alessandro

    (0) 
  11. Alessandro donati

    Hello

    first of all

    congratulations
    for the article.

      

    I have some
    problems getting this library within my DC, or rather in the local dc I
    imported but

     

    Development
    in Remote Configuration not find uwdjwf even if it exists in dir “/ usr /
    sap / <SID> / SYS / EPS / buildarchives”
    “UWLJWF05_0-20002656.SCA” the same package is deployed in our dtr and
    I can see it there.

        

    if I try to
    put the webdynpro I have on remote Configuration Development as a dependency in
    dipendencies it does not appear among the dcs that can be referenced, there I
    have only:

        

    ENFASCADE

    ENGINEAPI

    ESCONF_BUILDT

    FRAMEWORK

    MOINT_BUILDT

    SAP_BUILDT

    WD-RUNTIME

    WSRM

    Can you
    help me?

    Greetings Alessandro

    (0) 
    1. Jocelyn Dart

      Hi Alessandro, This blog was written 4 years ago. Much has changed since then. Please ask your question on the Business Process Management & Composition forum, and let us know the BPM or more likely Process Orchestration release and support pack you are on, and then we can help you further.

      Rgds,

      Jocelyn

      (0) 

Leave a Reply