Unit Test RetrieveAttributeResponse in CRM using Microsoft Fakes.


Hi,

Recently we wrote a plugin that was using RetrieveAttribute class to get the label as well as value of the optionset field.

The issue that we faced over here is that the AttributeMetadata property of RetrieveAttributeResponse is read only, we cannot set it. The following post provided the solution i.e. writing a wrapper class over the RetrieveAttributeResponse

http://www.alexanderdevelopment.net/post/2013/01/13/How-to-unit-test-C-Dynamics-CRM-interface-code-part-III


organizationService.ExecuteOrganizationRequest = request =>
 {
 var retrievedPicklistAttributeMetadata = new PicklistAttributeMetadata();

if (request.Parameters["LogicalName"].ToString() == "lss_service_request_status")
 {
 var optionMetadata = new OptionMetadata(new Label("Closed", 1033), 10);
 optionMetadata.Label.UserLocalizedLabel = new LocalizedLabel("Closed", 1033);
 optionMetadata.Label.UserLocalizedLabel.Label = "Closed";

var serviceRequestTypeOptionSet = new OptionSetMetadata
 {
 Name = "lss_service_request_status",
 DisplayName = new Label("Service Request Status", 1033),
 IsGlobal = false,
 OptionSetType = OptionSetType.Picklist,
 Options = { optionMetadata }
 };

retrievedPicklistAttributeMetadata.OptionSet = serviceRequestTypeOptionSet;
 }

var retrAttResponse = new RetrieveAttributeResponseWrapper(new RetrieveAttributeResponse());
 retrAttResponse.AttributeMetadata = retrievedPicklistAttributeMetadata;
 return retrAttResponse;
 };

Hope it helps!


2013 in review


The WordPress.com stats helper monkeys prepared a 2013 annual report for this blog.

Here’s an excerpt:

The Louvre Museum has 8.5 million visitors per year. This blog was viewed about 300,000 times in 2013. If it were an exhibit at the Louvre Museum, it would take about 13 days for that many people to see it.

Click here to see the complete report.

Microsoft Fakes and WebChannelFactory (Unit Test for WCF Service)


Hi,

Recently we wrote a plugin assembly that was calling a rest service using WebChannelFactory.


// wcf interface

[ServiceContract]

public interface IAutoNumber
 {
 /// <summary>
 /// Gets the next number.
 /// </summary>
 /// <param name="value">The value.</param>
 /// <returns>the next incremental value</returns>
 [WebGet(UriTemplate = "getsequencenumber/{value}")]
 [OperationContract]
 string GetNextNumber(string value);
 }

// plugin code

using (
 var webChannelFactory =
 new WebChannelFactory<IAutoNumber>(
 new Uri(dictionaryConfigSettings[Constants.AutoNumberServiceUrl])))
 {
 IAutoNumber client = webChannelFactory.CreateChannel();
 // get the next number from sequence
 autoNumber = Constants.PrefixClient +
 AddPadding(client.GetNextNumber(Constants.EntityNameClient),
 Constants.PrefixClientLength);

if (entity.Attributes.Contains("name"))
 {
 // add the value to the name property bag input parameter for the client record being created
 entity.Attributes["name"] = autoNumber;
 }
 else
 {
 entity.Attributes.Add("name", autoNumber);
 }
 }

Next we had to write the unit test case for the plugin using Microsoft Fakes. 
Below is the code that we used
</span></pre>
using (ShimsContext.Create())
 {
 ShimChannelFactory<IAutoNumber>.Constructor = factory => { };
 IAutoNumber autoNumber = new StubIAutoNumber
 {
 GetNextNumberString = s => { return "12345"; }
 };
 ShimChannelFactory<IAutoNumber>.AllInstances.CreateChannel = factory => { return autoNumber; };
 var preClientCreate = new PreClientCreate();
 preClientCreate.Execute(serviceProvider);
 }

Hope it helps.

Upload User Profile Picture programmatically in SharePoint 2013


Hi,

We recently had a requirement to create a web part that would be used to update user profile picture and few other properties.

For this we used the FIileUpload control.

The code used


public void UploadPhoto(string accountName, byte[] image)
 {

string loginNameForProfile = accountName.Substring(accountName.IndexOf(@"|") + 1);

if (profileUpload.HasFile)
 {
 SPSecurity.RunWithElevatedPrivileges(delegate()
 {
 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
 {
 SPServiceContext serviceContext = SPServiceContext.GetContext(site);
 UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
 ProfilePropertyManager profilePropMgr =
 new UserProfileConfigManager(serviceContext).ProfilePropertyManager;

// Retrieve all properties for the "UserProfile" profile subtype,
 // and retrieve the property values for a specific user.
 ProfileSubtypePropertyManager subtypePropMgr =
 profilePropMgr.GetProfileSubtypeProperties("UserProfile");
 UserProfile userProfile = userProfileMgr.GetUserProfile(loginNameForProfile);

 SPSite mySite = new SPSite(userProfileMgr.MySiteHostUrl);
 SPWeb web = mySite.RootWeb;
 SPFolder subfolderForPictures = web.Folders["User Photos"].SubFolders["Profile Pictures"];

web.AllowUnsafeUpdates = true;

Stream fs = profileUpload.PostedFile.InputStream;

byte[] buffer = new byte[profileUpload.PostedFile.ContentLength];
 fs.Read(buffer, 0, Convert.ToInt32(profileUpload.PostedFile.ContentLength));
 fs.Close();
 int largeThumbnailSize = 300;
 int mediumThumbnailSize = 72;
 int smallThumbnailSize = 48;

&nbsp;

string accName = accountName.Substring(accountName.IndexOf(@"\") + 1);
 using (MemoryStream stream = new MemoryStream(buffer))
 {
 using (Bitmap bitmap = new Bitmap(stream, true))
 {

 CreateThumbnail(bitmap, largeThumbnailSize, largeThumbnailSize, subfolderForPictures,
 accName + "_LThumb.jpg");
 CreateThumbnail(bitmap, mediumThumbnailSize, mediumThumbnailSize, subfolderForPictures,
 accName + "_MThumb.jpg");
 CreateThumbnail(bitmap, smallThumbnailSize, smallThumbnailSize, subfolderForPictures,
 accName + "_SThumb.jpg");
 }
 }
 string pictureUrl = String.Format("{0}/{1}/{2}_MThumb.jpg", subfolderForPictures.ParentWeb.Site.Url, subfolderForPictures.Url, accName);
 // Change the value of a single-value user property.
 userProfile[PropertyConstants.PictureUrl].Value = pictureUrl;
 imgUserProfile.ImageUrl = pictureUrl;
 // Save the changes to the server.
 userProfile.Commit();

web.AllowUnsafeUpdates = false;
 }
 });

}
 }

public SPFile CreateThumbnail(Bitmap original, int idealWidth, int idealHeight, SPFolder folder, string fileName)
 {
 SPFile file = null;

Assembly userProfilesAssembly = typeof(UserProfile).Assembly;

Type userProfilePhotosType = userProfilesAssembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfilePhotos");
 MethodInfo[] mi_methods = userProfilePhotosType.GetMethods(BindingFlags.NonPublic | BindingFlags.Static);

MethodInfo mi_CreateThumbnail = mi_methods[0];
 if (mi_CreateThumbnail != null)
 {
 file = (SPFile)mi_CreateThumbnail.Invoke(null, new object[] { original, idealWidth, idealHeight, folder, fileName, null });
 }

return file;
 } // end of function

The helpful post

http://lixuan0125.wordpress.com/2013/04/08/upload-user-profile-pictures-programmatically-sharepoint-2013/

Hope it helps

Cannot make a call on this channel because a call to Open() is in progress error in CRM 2011.


Hi,

We had developed a Visual Web Part for SharePoint 2013 that used IOrganizationService of CRM 2011. It was all working fine however yesterday it started throwing this error.

After some investigation we realized that the user whose credentials we were using as Client Credentials for the OrganizationService had recently been changed.

Updating it to the correct updated one fixed the issue for us.

Hope it helps.

Disable wsstracing.exe in SharePoint


Hi,

At times we need to disable wsstracing.exe to improve the system’s performance at it uses too much CPU resources.

Open Command Prompt as Administrator

To Disable

  • sc stop “SPTraceV4”
  • sc config “SPTraceV4” start= disabled

To Delete

  • sc delete “SPTraceV4”

bye.

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓