AUTENTICAZIONE MULTIAZIENDA CRM 4: esempio di codice

MULTIAZIENDA CRM 4

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 CrmDiscoveryService;
using CrmService;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region Retrieve all Organizations
 
            // This method only lists the current organizations
            // it will be used in Week 8's labs.
            GetOrganizations();
 
            // Hardcodeded for the CRM Titan CTP organization
            GetProjects("CRM Titan CTP");
 
            // Gets the current week to input a timesheet for
            SetupThisWeek();
 
            #endregion
        }
    }
    ///

    /// Retrieves all the CRM Organizations available on the server
    ///

    private void GetOrganizations()
    {
       //Authenticate with the DiscoveryService using
// Active Directory Windows Integrated Security
        CrmDiscoveryService.CrmDiscoveryService discoveryService = new CrmDiscoveryService.CrmDiscoveryService();
        discoveryService.Credentials = System.Net.CredentialCache.DefaultCredentials;
 
        //Make request for organization information
        RetrieveOrganizationsRequest orgsRequest = new RetrieveOrganizationsRequest();
 
        RetrieveOrganizationsResponse orgsResponse = (RetrieveOrganizationsResponse)discoveryService.Execute(orgsRequest);
 
 
        //Populate the organizations
        for (int i = 0; i < orgsResponse.OrganizationDetails.Length; i++)
        {
            ddlOrganizations.Items.Add(new ListItem(orgsResponse.OrganizationDetails[i].OrganizationName, orgsResponse.OrganizationDetails[i].OrganizationId.ToString()));
        }
    }
 
    ///

    /// Retrieve Projects where I am a Consultant on (Owner)
    ///

    private void GetProjects(string organizationName)
    {
        CrmService.CrmService crmService = GetCrmService(organizationName);
 
        QueryByAttribute query = new QueryByAttribute();
        ColumnSet cols = new ColumnSet();
        cols.Attributes = new string[] { "new_projectid", "new_name" };
 
        query.ColumnSet = cols;
        query.EntityName = EntityName.new_project.ToString();
        query.Attributes = new string[] { "ownerid" };
 
        WhoAmIRequest userRequest = new WhoAmIRequest();
        WhoAmIResponse user = (WhoAmIResponse)crmService.Execute(userRequest);
 
        // The logged on users userid
        query.Values = new object[] { user.UserId.ToString() };
 
        BusinessEntityCollection retrievedProjects = crmService.RetrieveMultiple(query);
 
        ddlProjects.Items.Add(new ListItem("Please select a Project", null));
 
        // Populate the Projects
        for (int i = 0; i < retrievedProjects.BusinessEntities.Length; i++)
        {
            ddlProjects.Items.Add(new ListItem(((new_project)retrievedProjects.BusinessEntities[i]).new_name, ((new_project)retrievedProjects.BusinessEntities[i]).new_projectid.Value.ToString()));
        }
 
    }
 
    ///

    /// Setup the labels to show dates for the current week
    ///

    private void SetupThisWeek()
    {
        DateTime today = DateTime.Now;
        DayOfWeek dayOfWeek = today.DayOfWeek;
 
        switch (dayOfWeek)
        {
            case DayOfWeek.Monday:
                Mon.Text = "Mon
" + today.Month + "/" + today.Day;
                Tue.Text = "Tue
" + today.AddDays(1).Month + "/" + today.AddDays(1).Day;
                Wed.Text = "Wed
" + today.AddDays(2).Month + "/" + today.AddDays(2).Day;
                Thu.Text = "Thu
" + today.AddDays(3).Month + "/" + today.AddDays(3).Day;
                Fri.Text = "Fri
" + today.AddDays(4).Month + "/" + today.AddDays(4).Day;
                break;
 
            case DayOfWeek.Tuesday:
                Mon.Text = "Mon
" + today.AddDays(-1).Month + "/" + today.AddDays(-1).Day;
                Tue.Text = "Tue
" + today.Month + "/" + today.Day;
                Wed.Text = "Wed
" + today.AddDays(1).Month + "/" + today.AddDays(1).Day;
                Thu.Text = "Thu
" + today.AddDays(2).Month + "/" + today.AddDays(2).Day;
                Fri.Text = "Fri
" + today.AddDays(3).Month + "/" + today.AddDays(3).Day;
                break;
 
            case DayOfWeek.Wednesday:
                Mon.Text = "Mon
" + today.AddDays(-2).Month + "/" + today.AddDays(-2).Day;
                Tue.Text = "Tue
" + today.AddDays(-1).Month + "/" + today.AddDays(-1).Day;
                Wed.Text = "Wed
" + today.Month + "/" + today.Day;
                Thu.Text = "Thu
" + today.AddDays(1).Month + "/" + today.AddDays(1).Day;
                Fri.Text = "Fri
" + today.AddDays(2).Month + "/" + today.AddDays(2).Day;
                break;
 
            case DayOfWeek.Thursday:
                Mon.Text = "Mon
" + today.AddDays(-3).Month + "/" + today.AddDays(-3).Day;
                Tue.Text = "Tue
" + today.AddDays(-2).Month + "/" + today.AddDays(-2).Day;
                Wed.Text = "Wed
" + today.AddDays(-1).Month + "/" + today.AddDays(-1).Day;
                Thu.Text = "Thu
" + today.Month + "/" + today.Day;
                Fri.Text = "Fri
" + today.AddDays(1).Month + "/" + today.AddDays(1).Day;
                break;
 
            case DayOfWeek.Friday:
                Mon.Text = "Mon
" + today.AddDays(-4).Month + "/" + today.AddDays(-4).Day;
                Tue.Text = "Tue
" + today.AddDays(-3).Month + "/" + today.AddDays(-3).Day;
                Wed.Text = "Wed
" + today.AddDays(-2).Month + "/" + today.AddDays(-2).Day;
                Thu.Text = "Thu
" + today.AddDays(-1).Month + "/" + today.AddDays(-1).Day;
                Fri.Text = "Fri
" + today.Month + "/" + today.Day;
                break;
 
            case DayOfWeek.Saturday:
                Mon.Text = "Mon
" + today.AddDays(-5).Month + "/" + today.AddDays(-5).Day;
                Tue.Text = "Tue
" + today.AddDays(-4).Month + "/" + today.AddDays(-4).Day;
                Wed.Text = "Wed
" + today.AddDays(-3).Month + "/" + today.AddDays(-3).Day;
                Thu.Text = "Thu
" + today.AddDays(-2).Month + "/" + today.AddDays(-2).Day;
                Fri.Text = "Fri
" + today.Month + "/" + today.Day;
                break;
 
            case DayOfWeek.Sunday:
                Mon.Text = "Mon
" + today.AddDays(-6).Month + "/" + today.AddDays(-6).Day;
                Tue.Text = "Tue
" + today.AddDays(-5).Month + "/" + today.AddDays(-5).Day;
                Wed.Text = "Wed
" + today.AddDays(-4).Month + "/" + today.AddDays(-4).Day;
                Thu.Text = "Thu
" + today.AddDays(-3).Month + "/" + today.AddDays(-3).Day;
                Fri.Text = "Fri
" + today.Month + "/" + today.Day;
                break;
 
        }
 
    }
 
    ///

    /// Get the CrmService reference by Organization
    ///

    ///
    ///
    private CrmService.CrmService GetCrmService(string organizationName)
    {
        // Setup the Authentication Token
        CrmService.CrmAuthenticationToken token = new CrmAuthenticationToken();
        token.OrganizationName = organizationName;
 
        CrmService.CrmService crmService = new CrmService.CrmService();
        crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
        crmService.CrmAuthenticationTokenValue = token;
 
        return crmService;
    }
}

Dettagli...