HOWTO: Programmatically Create Members in Site Server 3.0 Using Java

ID: Q232485


The information in this article applies to:


SUMMARY

This article describes how to programmatically create members in Site Server 3.0 Membership Directory, using Active Directory Service Interfaces (ADSI) 2.0, Java/COM integration, and J/Direct. This article is based on the following article in the Microsoft Knowledge Base and performs the same tasks using Java rather than VBScript:

Q192781 How to Programmatically Create Members in Site Server 3.0


MORE INFORMATION

When you create a new Member object, the following three properties must be set:

The first two, objectClass and cn, are set when the Create() method is called. The third, GUID, must explicitly be set. If these properties are not set properly, the call to SetInfo() will fail and the new Member object will not be created.

The following code demonstrates adding a member programmatically:

import activeds.*;
//import autoobj.*;
//Uncomment the above line if you want to use
//early binding for the GuidGen object.
import com.ms.com.*;
import com.ms.win32.*;

public class ADSISamp
{
  IADsContainer oADsContainer;
  IADs oADsNewUser;
  Object oGuidGen;
  Variant strGuid;
  String strLdapPath;
  
  public static void main (String[] args) throws Exception
  {
    ADSISamp app = new ADSISamp();
    app.run();
    System.in.read();
  }
  
  public void run()
  {
    try 
    {
      //The path to the ou=Members container.
      strLdapPath = "LDAP://localhost:5292/o=Microsoft/ou=Members";
      //Instantiate the GUID Generator that comes with Site Server
      //and store the GUID for use later on.
      //Using CoCreateInstance here, but could 
      //also use a wrapper from autoobj.dll
      //in the Bin\P&M directory of the Site Server installation.
      oGuidGen = createObject("Membership.GuidGen.1");
      //oGuidGen = (IGuidGen)new GuidGen();
      //strGuid.putString(((IGuidGen)oGuidGen).GenerateGuid());
      
      //Using the IDispatch pointer returned from createObject
      //via the Dispatch class to call GenerateGuid.
      //Once again, could also use early-binding via the wrapper class.
      //Use of IDispatch allows us to use this object with out
      //importing the wrapper classes for autoobj.dll.
      strGuid = Dispatch.call(oGuidGen, "GenerateGuid");
      //Bind to the container in which the Member will be created.
      //Note that this is an API call, and is declared native 
      //with other API J/Direct declarations at the bottom of this
      //source file.  It does, however, return a COM Interface
      //exposed by the activeds.dll and described in activeds.tlb,
      //both of which can be found in the %windir%\system32 directory.
      oADsContainer = (IADsContainer)ADsGetObject(strLdapPath, IADs.iid);

      //Create the new user object.  Note that the Create() method
      //returns an Interface pointer that maps to the IADs wrapper 
      //class in Java, which is the core ADSI COM Interface.
      oADsNewUser = (IADs)oADsContainer.Create("member", "cn=JohnDoe");
      
      oADsNewUser.Put("givenName", new Variant("John"));
      oADsNewUser.Put("sn", new Variant("Doe"));
      oADsNewUser.Put("userPassword", new Variant("password"));
      //No need for a new Variant as Dispatch.call returns a Variant.
      oADsNewUser.Put("GUID", strGuid);
      oADsNewUser.SetInfo();
      
      //Destroy the objects.
      ComLib.release(oGuidGen);
      ComLib.release(oADsNewUser);
      ComLib.release(oADsContainer);
    }
    catch (Exception ex)
    {
      User32.MessageBox(0,
                        ex.toString(),
                        "Java Exception",
                        0);
    }
  }
  
  // J/Direct native API method declarations.
  //*******************************************************
  
  /** @dll.import("activeds", ole) */ 
  public static native IUnknown ADsGetObject(String pathname,
                                             _Guid riid);
  
  /** @dll.import("ole32", ole) */ 
  public static native IUnknown CoCreateInstance(_Guid clsid, 
                                                 IUnknown punkOuter, 
                                                 int context, 
                                                 _Guid riid); 
  /** @dll.import("ole32", ole) */ 
  public static native _Guid CLSIDFromString(String str);
  
  public static _Guid IID_IUnknown = new _Guid("{00000000-0000-0000-C000-000000000046}");
  
  public static Object createObject(String str) 
  {
    return CoCreateInstance(CLSIDFromString(str), 
                            null, 
                            ComContext.INPROC_SERVER | ComContext.LOCAL_SERVER, 
                            IID_IUnknown); 
  }  
} 


REFERENCES

Microsoft Developer Network (MSDN) Library: "Active Directory Service Interfaces Version 2.0" located in SDK Documentation/Platform SDK/Networking and Distributed Services.

For more information, see the following article in the Microsoft Knowledge Base:

Q192781 How to Programmatically Create Members in Site Server 3.0
For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, please see the following pages on the Microsoft Technical Support site:
http://support.microsoft.com/support/visualj/

http://support.microsoft.com/support/java/

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Robert LaCasse, Microsoft Corporation

Additional query words:


Keywords          : kbCOMt kbJava kbJNative kbSDKJava kbVJ600 kbGrpJava 
Version           : WINDOWS:3.1,6.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: June 10, 1999