20 May 2007

Integrate CRM with WSS 3.0 Picture Library

I worked for a project which needs integrate CRM with WSS 3.0 picture library.
The requirement was: Each account has it's own picture library in WSS, so the pictures could be use in CRM report etc.
Here's the code:


public partial class _Default : System.Web.UI.Page
{
string accountGUID = "";
protected void Page_Load(object sender, EventArgs e)
{
string entityId = Request.QueryString["oId"];
Guid accountId = (entityId == null) ? (new Guid("00000000-0000-0000-0000-000000000000")) : new Guid(entityId);
accountGUID = accountId.ToString();
string url = "http://WSS:6666/PL/" + accountGUID; // PL is a picture library in WSS 3.0

if (CheckUrl(url))
{
Response.Redirect(url);
}
else
{
Label1.Text = "This account doesn't has a Pictures Library, please create a one for it.";
}
}

public static bool CheckUrl(string url)
{
//check if the url(pictures library) is existing
HttpWebResponse httpResponse = null;

try
{
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
return (httpResponse.StatusCode == System.Net.HttpStatusCode.OK);
}
catch (Exception ex)
{
return false;
}
}

protected void Button1_Click(object sender, EventArgs e)
{
//Response.Redirect("http://WSS:6666/PL/Forms/Upload.aspx?Type=1");
SPSite Site = new SPSite("http://WSS:6666"); //site url
SPWeb Web = Site.OpenWeb();
Site.AllowUnsafeUpdates = true;
Web.AllowUnsafeUpdates = true;
SPFolder rootFolder = Web.GetFolder("PL"); // PL is a picture library in WSS 3.0
rootFolder.SubFolders.Add(accountGUID);
Page_Load(null, null);
}
}

05 May 2007

Accessing CRM database from JScript

Here's the code how to accesss CRM database via JScript!


//initial connection
var connection = new ActiveXObject("ADODB.Connection");
var connectionString = "Provider=SQLOLEDB;Server=_db;Database=_mscrm;Integrated Security=sspi";
//open connection
connection.Open(connectionString);
//query string
var query = "SELECT name FROM FilteredAccount";
//create a ADO object
var rs = new ActiveXObject("ADODB.Recordset");
//open connection
rs.Open(query, connection, 1, 2);
rs.moveFirst();
var values = "";
//read data
while (!rs.eof)
{
values += rs.Fields(0).Value.toString() + " ";
rs.moveNext();
}
//close connection
connection.Close();
//alert value
alert(values);