1: using System;
2: using System.IO;
3: using System.Text;
4: using System.Xml;
5: using Microsoft.Crm.Callout;
6: using DocList.CrmSdk;
7:
8: namespace crmsp.libraries
9: { 10: public class doclist : CrmCalloutBase
11: { 12:
13: public static System.Xml.XmlNode CreateFolder(string listName, string folderName)
14: { 15: //Creating a SharePoint document folder under a library requires us to pass an Xml document to the web service
16: //
17: // The XML document looks like this:
18: //
19: // <Batch OnError="Return" RootFolder="">
20: // <Method ID="1" Cmd="New">
21: // <Field Name="ID">New</Field>
22: // <Field Name="FSObjType">1</Field>
23: // <Field Name="BaseName">foldername</Field>
24: // </Method>
25: // </Batch>
26: // Let's set up an XML document and populate it with the settings for our new document folder
27: System.IO.StringWriter sw = new System.IO.StringWriter();
28: System.Xml.XmlTextWriter xw = new System.Xml.XmlTextWriter(sw);
29: xw.WriteStartDocument();
30:
31: // build batch node
32: xw.WriteStartElement("Batch"); 33: xw.WriteAttributeString("OnError", "Return"); 34: xw.WriteAttributeString("RootFolder", ""); 35:
36: // Build method node
37: xw.WriteStartElement("Method"); 38:
39: // Set transaction ID
40: xw.WriteAttributeString("ID", Guid.NewGuid().ToString("n")); 41: xw.WriteAttributeString("Cmd", "New"); 42:
43: // Build field ID
44: xw.WriteStartElement("Field"); 45: xw.WriteAttributeString("Name", "ID"); 46: xw.WriteString("New"); 47: xw.WriteEndElement(); // Field end
48:
49: // Build FSObjType
50: xw.WriteStartElement("Field"); 51: xw.WriteAttributeString("Name", "FSObjType"); 52: xw.WriteString("1"); // 1= folder 53: xw.WriteEndElement(); // Field end
54:
55: // Build Base Name field from folder
56: xw.WriteStartElement("Field"); 57: xw.WriteAttributeString("Name", "BaseName"); 58: xw.WriteString(folderName);
59: xw.WriteEndElement(); // Field end
60:
61: // Close Method & Batch elements
62: xw.WriteEndElement(); // Method end
63: xw.WriteEndElement(); // Batch end
64: xw.WriteEndDocument();
65: System.Xml.XmlDocument batchElement = new System.Xml.XmlDocument();
66: batchElement.LoadXml(sw.GetStringBuilder().ToString());
67:
68: //Setup web service
69: Lists listService = new Lists();
70: listService.Credentials = new System.Net.NetworkCredential("username", "password", "domain name"); 71:
72: // send update request to sharepoint to create the document folder
73: System.Xml.XmlNode result = listService.UpdateListItems(listName, batchElement);
74: return result;
75: }
76:
77: public override void PostCreate(CalloutUserContext userContext, CalloutEntityContext entityContext, string postImageEntityXml)
78: { 79: //SharePoint Document Libraries have a type code of 101
80: Int32 spDocLibraryListType = 101;
81:
82: //If this is a create of an account then create a document library
83: if (entityContext.EntityTypeCode == 1)
84: { 85: // Load the postImageEntityXml and use an XPath
86: // query to locate the name field value.
87: XmlDocument xd = new XmlDocument();
88: xd.LoadXml(postImageEntityXml);
89: // Initialize a NameTable object.
90: NameTable nt = new NameTable();
91: // Initialize a namespace manager.
92: XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
93: // Add the required prefix/namespace pairs to the namespace
94: // manager. Add a default namespace first.
95: nsmgr.AddNamespace("crm","http://schemas.microsoft.com/crm/2006/WebServices"); 96: nsmgr.AddNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance"); 97: XmlNode node = xd.SelectSingleNode("//crm:Property[@Name='name']/crm:Value", nsmgr); 98: // Grab the node's InnerText property to name our SharePoint Document Library.
99: if (node != null)
100: { 101: string accountName = node.InnerText;
102: //string listName = node.InnerText + "_" + DateTime.Now.ToString("yyyyMMdd_hhmmss"); 103: //Call the SharePoint Web Service to create a document library
104: Lists listService = new Lists();
105: listService.Credentials = new System.Net.NetworkCredential("user name", "password", "domain name"); 106: //System.Xml.XmlNode result = listService.AddList(listName, accountName + " Document Library", spDocLibraryListType);
107: System.Xml.XmlNode result = listService.AddList(accountName, accountName + " Document Library", spDocLibraryListType);
108: //grab the return xml
109: string returnXml = result.InnerXml.ToString();
110: //update Microsoft CRM record with the return xml
111: CrmService service = new CrmService();
112: service.Credentials = System.Net.CredentialCache.DefaultCredentials;
113:
114: // Create the account object.
115: account account = new account();
116:
117: // Set the properties of the account object to be updated.
118: account.new_sharepointdocumentlibraryurl = "http://yoursharepointservername/" + accountName + "/Forms/AllItems.aspx";
119:
120: // accountid is a key that references the ID of the account to be updated.
121: account.accountid = new Key();
122:
123: // account.accountid.Value is the GUID of the record to be changed.
124: account.accountid.Value = entityContext.InstanceId;
125:
126: // Update the account.
127: service.Update(account);
128: }
129: }
130:
131: //If this is a create of an opportunity then create a folder under the account's document library
132: if (entityContext.EntityTypeCode == 3)
133: { 134: //If the Opportunity is linked to an account then we want to create a document folder
135: //
136: // Load the postImageEntityXml and use an XPath
137: // query to locate the name field value.
138: XmlDocument xd = new XmlDocument();
139: xd.LoadXml(postImageEntityXml);
140: // Initialize a NameTable object.
141: NameTable nt = new NameTable();
142: // Initialize a namespace manager.
143: XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
144: // Add the required prefix/namespace pairs to the namespace
145: // manager. Add a default namespace first.
146: nsmgr.AddNamespace("crm", "http://schemas.microsoft.com/crm/2006/WebServices"); 147: nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 148: XmlNode node = xd.SelectSingleNode("//crm:Property[@Name='customerid']/crm:Value[@type='account']", nsmgr); 149: // Grab the node's InnerText property to see if this opportunity is related to an account.
150: if (node != null)
151: { 152: //this node gives us the Guid of the Account record
153: string strAccountId = node.InnerText;
154: //we now need to query the account to grab the SharePoint document library name
155: CrmService service = new CrmService();
156: service.Credentials = System.Net.CredentialCache.DefaultCredentials;
157:
158: //we'll also grab the Title of the sales Opportunity for naming our SharePoint folder
159: XmlNode nodeTitle = xd.SelectSingleNode("//crm:Property[@Name='name']/crm:Value", nsmgr); 160:
161: // Create the column set object that indicates the fields to be retrieved.
162: ColumnSet cols = new ColumnSet();
163: // Set the properties of the column set.
164: cols.Attributes = new string[] { "new_sharepointdocumentlibraryname" }; 165: // accountGuid is the GUID of the record being retrieved.
166: Guid accountGuid = new Guid(strAccountId);
167: // Retrieve the account.
168: // The EntityName indicates the EntityType of the object being retrieved.
169: account account = (account)service.Retrieve(EntityName.account.ToString(), accountGuid, cols);
170:
171: //OK, now we have the sharepoint document library name we can create a document folder under it
172: //the folder name will be "Sales Opp" + the Guid of the Opportunity record
173: //string strFolderName = "Sales Opp - " + nodeTitle.InnerText + "_" + DateTime.Now.ToString("yyyyMMdd_hhmmss"); 174: string strFolderName = "Sales Opp - " + nodeTitle.InnerText;
175: XmlNode xmlResult = CreateFolder(account.new_sharepointdocumentlibraryname, strFolderName);
176:
177: //Finally, we need to update the opportunity with the folder location
178: // Create an opportunity object.
179: opportunity opportunity = new opportunity();
180:
181: // Set the properties of the opportunity object to be updated.
182: opportunity.new_sharepointdocumentlibraryurl = "http://yoursharepointserver/" + account.new_sharepointdocumentlibraryname + "/Forms/AllItems.aspx?RootFolder=/" + account.new_sharepointdocumentlibraryname + "/" + strFolderName;
183:
184: // opportunityid is a key that references the ID of the opportunity to be updated.
185: opportunity.opportunityid = new Key();
186:
187: // opportunity.Value is the GUID of the record to be changed.
188: opportunity.opportunityid.Value = entityContext.InstanceId;
189:
190: // Update the opportunity.
191: service.Update(opportunity);
192: }
193: }
194: }
195: }
196: }