开发者

Programatically get a list of all workspaces (shared+Personal) in VisualForce Page

开发者 https://www.devze.com 2023-03-17 16:36 出处:网络
I have created a custom visualforce page. H开发者_高级运维ere in the controller class i want to get a list of all workspaces in a dropdown so that user can make a choice among where to publish the dat

I have created a custom visualforce page. H开发者_高级运维ere in the controller class i want to get a list of all workspaces in a dropdown so that user can make a choice among where to publish the data

Can somebody provide me some guidelines on how to achieve this.

Any help is appreciated. :)


You need to query the content workspaces and then put them in select option list which can then be referenced in a visualforce page.

Apex Controller:

public Id selectedWorkspaceId { get; set; }

private List<SelectOption> workspaceOptions;
public List<SelectOption> getWorkspaceOptions() {
 if(workspaceOptions == null) {
  for(ContentWorkSpace workspace : [select id, name from ContentWorkspace]) {
   workspaceOptions.addNewSelectOption(
    new SelectOption(workspace.id, workspace.name);
   }
  }
 }
 return workspaceOptions;
}

Visualforce Page:

<apex:selectList value="{!selectedWorkspaceId}" size="1">
 <apex:selectOptions value="{!workspaceOptions}"/>
</apex:selectList>

Documentation:

  • SelectOption Class
  • <apex:selectList> component
  • <apex:selectOptions> Component
  • ContentWorkSpace object


Below is Java code (from code reciepe) to Query a List of Workspaces in Salesforce CRM Content. Translate below code to apex and it should work.

package com.sforce;

import org.apache.axis.client.Stub;

import com.sforce.soap.enterprise.*;
import com.sforce.soap.enterprise.sobject.*;

public class QueryWorkspace {

    /**
     * @param args
     */  

    public static void main(String[] args) {
        QueryWorkspace queryWorkspace = new QueryWorkspace();
        try {
            queryWorkspace.queryAvailableWorkspaces();
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
    }

    private void queryAvailableWorkspaces() throws Exception {

        SforceServiceLocator sforceServiceLocator
            = new SforceServiceLocator();

        SoapBindingStub binding
            = (SoapBindingStub) sforceServiceLocator.getSoap();

        // Login  

        LoginResult lr = binding.login("user@jstest.org",
                "password");

        binding._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,
                lr.getServerUrl());

        // Create a session header for subsequent calls  

        _SessionHeader sessionHeader = new _SessionHeader();
        sessionHeader.setSessionId(lr.getSessionId());
        binding.setHeader(sforceServiceLocator.getServiceName()
                .getNamespaceURI(), "SessionHeader", 
        sessionHeader);

        QueryResult query
            = binding.query("select Id, Name from" 
            + " ContentWorkspace");

        SObject[] records = query.getRecords();
        if (records != null) {
            for (int i = 0; i < records.length; i++) {
                ContentWorkspace contentWorkspace =
                    (ContentWorkspace)records[i];
                System.out.println("Workspace Id:"
                        + contentWorkspace.getId() + " Name:"
                        + contentWorkspace.getName());
            }
        }        
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号