Here is a code sample which will clone a virtual machine from a given template. I have also shown how to get the “MAC Address” of the newly created VM. Here is sample usage of the code

CreateClone <IP Address of XenServer> <UserName> <Password> <Template>
<span class="code-comment">/// &lt;summary&gt;
</span><span class="code-comment">/// Clone a Virtual machine from a template and determine virtual machines mac address.
</span><span class="code-comment">/// &lt;/summary&gt;
</span>    <span class="code-keyword">public</span> class Program
        <span class="code-keyword">public</span> <span class="code-keyword">static</span> void Main(string[] args)

            <span class="code-comment">// Host information necessary to get started
</span>            string hostname = args[0];
            <span class="code-object">int</span> port = 80; <span class="code-comment">// <span class="code-keyword">default</span>
</span>            string username = args[1]; ;
            string password = args[2];
            string template = args[3];

            <span class="code-comment">// Establish a session
</span>            Session session = <span class="code-keyword">new</span> Session(hostname, port);

            <span class="code-comment">// Authenticate with username and password.
</span><span class="code-comment">//The third parameter tells the server which API
</span><span class="code-comment">//version we support.
</span>            session.login_with_password(username, password, API_Version.API_1_3);

            List&lt;XenRef&lt;VM&gt;&gt; vmRefs = VM.get_by_name_label(session, template);
            <span class="code-keyword">if</span> (vmRefs.Count == 0)
                <span class="code-object">System</span>.Console.WriteLine(<span class="code-quote">"Template not found"</span>);

            foreach (XenRef&lt;VM&gt; vmRef in vmRefs)
                <span class="code-keyword">if</span> (vmRefs.Count == 1)
                    VM vm = VM.get_record(session, vmRef);
                    <span class="code-object">System</span>.Console.WriteLine(<span class="code-quote">"Cloning VM '{0}'..."</span>, vm.name_label);
                    XenRef&lt;VM&gt; cloneVMref = VM.clone(session, vmRef,
                    string.Format(<span class="code-quote">"Cloned VM (from '{0}')"</span>,vm.name_label));
                    <span class="code-object">System</span>.Console.WriteLine(<span class="code-quote">"Cloning VM '{0}'... Done"</span>, cloneVMref.ToString());
                    VM.provision(session, cloneVMref);
                    VM CloneVM = VM.get_record(session, cloneVMref);

                    foreach (XenRef&lt;VIF&gt; vifref in CloneVM.VIFs)
                        <span class="code-object">System</span>.Console.WriteLine(VIF.get_MAC(session, vifref));
                <span class="code-keyword">else</span> { <span class="code-object">System</span>.Console.WriteLine(<span class="code-quote">"More then one VM Template found with same name"</span>); }


On the other note XenServer 5.0 SDK has been released on the CDN and you can download the sample codes here

Please let me know what other samples would you like to see on the CDN regarding XenServer. If we had to do a webinar on XenServer API what would you like to hear about?