Technical Fun

Holla holla, CRM Party dudes, it’s me again in a very mundane snippet series of CRM 4.0.
This time, we’re gonna grab the content from a picklist attribute and show it to the world! (Well, usually they end up only seeing an asp.net web app, but whatever ..)

Beware, this one’s an overkill scenario, the essence is somewhere within. Seek. :)

First things first, I like to create an interface that should build the structure of a picklist object (Why? because .. just because).

1 interface IPicklistObject 2 { 3 int Id {get; set;} 4 string Name {get; set;} 5 }

Then, if an entity got many picklist attributes, we’ll create each class that implement that interface, just like that. (i.e: This SeverityLevel class below).

1 public class SeverityLevel : IPicklistObject 2 { 3 4 #region IPicklistObject Members 5 6 public int Id 7 { 8 get; 9 set; 10 } 11 12 public string Title 13 { 14 get; 15 set; 16 } 17 18 #endregion 19 }

Further more, if you have many picklist-type attributes on your entity, you might want to do mapping externally. My method of choice was a custom xml file that holds to map, like this:
(We’ll need this to pass to the Activator later ..[why??? sshhh ....!])

1 <configuration> 2 <settings> 3 <attributeName> 4 <severitylevel>Full.Name.Of.Your.Concrete.Class.SeverityLevel</severitylevel> 5 </attributeName> 6 </settings> 7 </configuration>

<severitylevel> tag is actually the name of the picklist attribute in crm.
Well of course as a bonus snippet, here’s what I do to read this XML;

1 private static string getMappedValueOf(string attributename) 2 { 3 XmlDocument xDoc = new XmlDocument(); 4 string sFileName = YOUR_XML_FILE_LOCATION; 5 6 if (File.Exists(sFileName) == false) 7 { 8 return string.Empty; 9 } 10 11 try 12 { 13 xDoc.Load(sFileName); 14 XmlNode theNode = xDoc.SelectSingleNode("/configuration/settings/attributeName/" + attributename); 15 if (theNode != null) 16 return theNode.InnerText; 17 18 xDoc = null; 19 return string.Empty; 20 } 21 catch 22 { 23 xDoc = null; 24 } 25 26 return string.Empty; 27 }

Ok, if you are somewhat still following this guide [ I really have no idea why you do that ], you will need to create this method;

1 private static IPicklistObject CreateFrom(string attributename, int key, string name) 2 { 3 Type atttype = Type.GetType(getMappedValueOf(attributename.ToLower()), false); 4 IPicklistObject result = (IPicklistObject)Activator.CreateInstance(atttype); 5 6 result.Id = key; 7 result.Title = name; 8 9 return result; 10 }

Last and final thing, a method that will fetch the picklist attribute using the metadata service (if you’re not like me, this should be the only part you read).

1 public static IList<IPicklistObject> getPickListValue(string entityName, string pickListAttributeName) 2 { 3 RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest(); 4 attributeRequest.EntityLogicalName = entityName; 5 attributeRequest.LogicalName = pickListAttributeName; 6 attributeRequest.RetrieveAsIfPublished = true; 7 8 RetrieveAttributeResponse attributeResponse = 9 (RetrieveAttributeResponse)metaService.Execute(attributeRequest); 10 11 PicklistAttributeMetadata pl = 12 (PicklistAttributeMetadata)attributeResponse.AttributeMetadata; 13 14 IList<IPicklistObject> resultset = new List<IPicklistObject>(); 15 16 foreach (Option o in pl.Options) 17 { 18 resultset.Add(CreateFrom(pickListAttributeName,o.Value.Value,o.Label.UserLocLabel.ToString())); 19 } 20 21 return resultset; 22 }

So at this point, you can just pass this collection of picklist object to a control, (a drop down list in my case), and be happy.

Tags:

Holla, CRM Party people, to entertain your greed towards CRM snippets and to keep me from forgetting such a trivial thing, here we go … the adding attachment / note / annotation snippet for CRM 4.0. (In this sample I am binding it to the incident entity).

1 public static bool AddAttachment(string filename, Guid incidentguid, byte[] modal, out string errormessage) 2 { 3 try 4 { 5 string encodedData = Convert.ToBase64String(modal); 6 7 annotation ano = new annotation(); 8 ano.filename = filename; 9 ano.isdocument = new Petrosea.CRM.SD.MetaUtility.CRMWebService.CrmBoolean(); 10 ano.isdocument.Value = true; 11 ano.documentbody = encodedData; 12 ano.mimetype = "application/octet-stream"; 13 ano.objectid = new Lookup(); 14 ano.objectid.type = EntityName.incident.ToString(); 15 ano.objectid.Value = incidentguid; 16 ano.objecttypecode = new EntityNameReference(); 17 ano.objecttypecode.Value = EntityName.incident.ToString(); 18 19 TargetCreateAnnotation target = new TargetCreateAnnotation(); 20 target.Annotation = ano; 21 CreateRequest createrequest = new CreateRequest(); 22 createrequest.Target = target; 23 CreateResponse created = (CreateResponse)crmService.Execute(createrequest); 24 25 errormessage = String.Empty; 26 return true; 27 } 28 catch (Exception ex) 29 { 30 errormessage = ex.Message; 31 return false; 32 } 33 }

Tags:

Holla, Ok, I am new to the world of CRM 4.0 Plug-Ins, and even though I wrote many callouts for CRM 3.0, that does not count to the experience. So, I am a flat newbie :) [excuse mode ON].

Here’s the story; I was trying to hook to a post update event of the opportunity entity, and naturally wanting to do something based on the opportunityid I (should have) obtained easily.

Here’s the un-easy part;

Microsoft.Crm.Sdk.DynamicEntity opp = context.PostEntityImages["opportunity"] as Microsoft.Crm.Sdk.DynamicEntity;

and afterwards, I could call the Id (GUID) of the opportunity like this;

Microsoft.Crm.Sdk.Key okey = opp["opportunityid"] as Microsoft.Crm.Sdk.Key; Guid oppid = okey.Value;

Hmm… before I got here, I tried to use and register PreEntityImages, but failed. I tried to use OutputParameters of the context, and failed as well.

So, for post events, I’ll stick to this PostEntityImages thing.

Tags:

As usual I have a very unimportant need of getting the list of every site collections that a user has permissions on my company’s MOSS 2007 environment. The list is then to be passed to another application, for other not so important cause.

My company’s site collection structure looks like:
- http://rootsite
- http://rootsite/sites/site01
- http://rootsite/sites/site02
- etc ….

So a the list needs to list those site where a user got permission to access. (user A –> site01, site02, … user B –> site01, etc)

So, assuming that each site collection defines user permissions directly (not inside a group, which happens to be the case in my environment), I wrote this cheap thing…

public List<string> GetValidSites(string sitecollectionname, string userloginname) { SPSite sitecol = new SPSite(sitecollectionname); List<string> siteList = new List<string>(); foreach (SPSite s in sitecol.WebApplication.Sites) { if (s.Url.Contains("/sites/")) { if (IsAllowedFor(userloginname, s.Url)) { siteList.Add(s.Url); } } } return siteList; }

You get the dumbest idea just by looking at it, right? Next, we dig further;

private bool IsAllowedFor(string userloginname, string siteurl) { bool result = false; SPSite site = new SPSite(siteurl); SPWeb web = site.OpenWeb(); try { SPUser usr = web.AllUsers[userloginname]; result = true; } catch { result = false; } return result; }

Host it on web service or DLL, or whatever.
Then, you can call something like this on your client app:

GetValidSites("http://rootsite","domainname\\username");

Happy? I am .. :)

Well, not exactly removing, just crippling it so it won’t function. Hahah…

It all began with a guy in another department who got this persistent demand of getting rid of the single file upload button from a SharePoint document library upload menu. “We can upload single file from the multiple one, no need to have a single one.” He claimed. Hahah..

Given that I am a world-class-lazy-bum, I googled, but found that some working solutions applied globally towards the entire site collection, and not strictly to one doclib inside one site. Some claimed they have managed to do so by doing various tricks. But again, due to my acute laziness, I decided not to play around with Features, or even those object models, instead, I resorted to JavaScript.

What I did was described in Lazy 101; detect the Upload Menu by it’s many possible identifier (in this case i use its’ “text” attribute), and brutally disabled it. I cunningly inserted a content editor webpart in the page where the doclib tool bar was visible, and chuckled in an insane glee. Mwhuahahahhahuahahahah …

The script is as below:

<script type="text/javascript"> function GetElementByText(tagName, title) { var a = document.getElementsByTagName(tagName); for (var i=0; i < a.length; i++) { if (a[i].text) { if (a[i].text === title) { return a[i]; } } } return null; } if (window.onload) { var oLoad = window.onload; window.onload = function bodyLoad() { oLoad(); var o = GetElementByText("ie:menuitem","Upload Document"); if (o) { o.disabled = true; } } } </script>

[Update] Hey, guess what, a very kind guy has created a better tool for this (he’s even “featurize” it), check it here

Suddenly my team mate got this strange need to prevent folder creation inside MOSS document libraries. So I said “Aah .. “, and contemplated.

The logic should be simple, intercept the creation process, detect if the item being added to the doclib is really a folder, and cancel the event.
But the reality, the ItemAdding event wouldn’t let me do the detection process.

So, a brutal method was then picked :)
I resorted to the ItemAdded thing, and unleashed this evil;

public override void ItemAdded(SPItemEventProperties properties) { if (properties.ListItem.ContentType.Name == "Folder") { properties.ErrorMessage = "You can't create folders"; properties.ListItem.Delete(); } }

It worked. so stupidly enough, this will wipe the folder that you just created after detecting that the item you just added was a folder.

My two cents guys, who knows this might bail you out of jail too! :)

This one’s a sequel to my last post about LINQ-ize a SPListCollection.
I got another not-so-important urge to get a maximum number from one of my custom list’s field.
Let’s just say the the field containing the number is “Document Number”, and I want to grab the current maximum value of that field to do various purposes.

Now, as I am a little bit obsessed with Linq, so let’s leave CAML behind and grab Linq instead.
Let’s begin by creating a wrapper around SPListItemCollection object to make it Linq-able (just like I did with the SPListCollection).

public class SPListItemCollectionAdapter : List<SPListItem>
{
private SPListItemCollection _listItemCollection;

public SPListItemCollectionAdapter(SPListItemCollection listItemCollection)
{
_listItemCollection = listItemCollection;

Refresh();
}

private void Refresh()
{
this.Clear();

foreach (SPListItem item in _listItemCollection)
{
this.Add(item);
}
}

}

then voila!, you can use Linq’s aggregation function to grab the max value currently on a field called “Document Number” inside a list called “Document”. Write a method that might look like this:

private static double GetMaxFrom(string listname, string fieldname)
{
SPListItemCollection itemcol = mossWeb.Lists[listname].Items;
SPListItemCollectionAdapter itemsAdapter = new SPListItemCollectionAdapter(itemcol);
var result = itemsAdapter.Max(x => x[fieldname]);
return (double)result;
}

mossWeb is just a singleton for the SPWeb object, as I was using it numerous times on other methods as well.
I also took the liberty of using the lambda expression (x => x[fieldname]) just to evaluate the specific field.
Using it is as easy as calling GetMaxFrom("Document","Document Number");

Follow

Get every new post delivered to your Inbox.