<system-xml><!--#START-ROOT-CODE
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
	string url = "";
    string username = "";
    string password = "";
	
    protected void Page_Load(object sender, EventArgs e)
    {
/* Add [system-asset-page] tags around filename and give specific folder path. Do not add .php file extension after [/system-asset:page] */
        string configFileName = "[system-asset:page]/test/files/access-rights-config.xml[/system-asset:page]";
        
        string fullPathStart = Request.ServerVariables.Get("PATH_TRANSLATED");
        string currentPath = fullPathStart.Substring(0,fullPathStart.LastIndexOf('\\')+1);	
        int i;

        XmlTextReader configFile = new XmlTextReader(currentPath + configFileName); 
        string cmsAddress = "";
        
        while (configFile.Read())
        {
            switch (configFile.NodeType)
            {
                case XmlNodeType.Element:                  
                    if (configFile.Name.Equals("cmsAddress"))
                        cmsAddress = configFile.ReadString();                    
                 break;
            }
        }
        cmsAddress = cmsAddress + "/ws/services/AssetOperationService?wsdl";
        url = cmsAddress;
        string username = "_system_all_groups";
        string password = "systemuser";
	    
/* Get current users from CMS */
	    ArrayList groups = getUserGroups(username);
        string xmlContent = "<dropdown multiple=\"true\">";
        
        for(i = 0; i<groups.Count;i++)
        {
	        if (i==0)
		        xmlContent+="<item default=\"true\">";
	        else
		        xmlContent+="<item>";
	        xmlContent+=groups[i];
	        xmlContent+="</item>";
        }
        xmlContent+="</dropdown>";
        
        editMetadataSet(xmlContent);

        main.Text += "done";
    }
	
    void editMetadataSet(string xml)
    {
        wsdl.AssetOperationHandlerService ser = new wsdl.AssetOperationHandlerService();
        ser.Url = url;

        wsdl.authentication auth = new wsdl.authentication();
        auth.username = "_system_all_groups";
        auth.password = "systemuser";

        wsdl.asset myAsset = new wsdl.asset();
        wsdl.metadataSet mySet = new wsdl.metadataSet();
        mySet.name = "Intranet";
        mySet.parentContainerPath = "/General";
        mySet.path = "/General/Intranet";
        mySet.displayNameFieldVisibility = wsdl.metadatafieldvisibility.inline;
        mySet.titleFieldVisibility = wsdl.metadatafieldvisibility.inline;
        mySet.summaryFieldVisibility = wsdl.metadatafieldvisibility.inline;
        mySet.keywordsFieldVisibility = wsdl.metadatafieldvisibility.inline;
        wsdl.dynamicMetadataFieldDefinition[] fields = new wsdl.dynamicMetadataFieldDefinition[1];
        wsdl.dynamicMetadataFieldDefinition field = new wsdl.dynamicMetadataFieldDefinition();
        field.name = "Audience Access Rights";
        field.fieldType = wsdl.dynamicmetadatafieldtype.dropdown;
        field.required = false;
        field.visibility = wsdl.metadatafieldvisibility.inline;
        field.configurationXML = xml;
        fields[0] = field;
        mySet.dynamicFields = fields;
        myAsset.Item = mySet;
        ser.edit(auth, myAsset);
    }

	ArrayList getUserGroups(string usernameToGet)
    {        
        wsdl.AssetOperationHandlerService ser = new wsdl.AssetOperationHandlerService();
        ser.Url = url;

		wsdl.authentication auth = new wsdl.authentication();
        auth.username = "_system_all_groups";
        auth.password = "systemuser";

        wsdl.identifier id = new wsdl.identifier();
        id.id = usernameToGet;
        id.type = wsdl.entityTypeString.user;
        wsdl.readResult res = ser.read(auth, id);
        wsdl.user user = res.asset.user;
        string groupString = user.groups;
        ArrayList groups = new ArrayList();
        while(groupString.IndexOf(";")!=-1)
        {
            groups.Add(groupString.Substring(0,groupString.IndexOf(";")));
            groupString = groupString.Substring(groupString.IndexOf(";")+1);
        }
        groups.Add(groupString);
        return groups;
    }		
}
//#END-ROOT-CODE--></system-xml>