Posts

Showing posts from 2017

Backup-SPSite : You must specify a filename for the backup file.

I followed the backup instructions from  here  to backup my site collection but I encountered the following error: You must specify a filename for the backup file That was strange as I have set the -Path parameter. The resolution is that the path MUST BE VALID . In my case , I was missing an underscore.

SharePoint 2010: Unable to open Central Administration

I have been trying to install a SharePoint 2010 development environment on my local Windows 10 laptop (dont ask why) and I have finally been able to load Central Admin. Here are the issues I encountered: 1. Ensure you have Windows 10 Pro - you will need to activate Windows Auth before you install 2. Run the following script if Central Admin is start /w pkgmgr /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-StaticContent;IIS-DefaultDocument;IIS-DirectoryBrowsing;IIS-HttpErrors;IIS-ApplicationDevelopment;IIS-ASPNET;IIS-NetFxExtensibility;IIS-ISAPIExtensions;IIS-ISAPIFilter;IIS-HealthAndDiagnostics;IIS-HttpLogging;IIS-LoggingLibraries;IIS-RequestMonitor;IIS-HttpTracing;IIS-CustomLogging;IIS-ManagementScriptingTools;IIS-Security;IIS-BasicAuthentication;IIS-WindowsAuthentication;IIS-DigestAuthentication;IIS-RequestFiltering;IIS-Performance;IIS-HttpCompressionStatic;IIS-HttpCompressionDynamic;IIS-WebServerManagementTools;IIS-ManagementConsole;IIS-IIS6ManagementCompatibili

SharePoint 2010 on Windows 10: Solving COMException / Unknown error (0x80005000)

Image
This is not a problem I expected to solve, but it happened anyway. A client is using SharePoint 2010 and I need a local development farm. I started with the usual configuration requirements: Add <Setting Id="AllowWindowsClientInstall" Value="True"/> to the Setup config Created a script to make a new configuration database so that I dont have to join a domain $secpasswd = ConvertTo-SecureString "MyVerySecurePassword" -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential ("mydomain\administrator", $secpasswd) $guid = [guid]::NewGuid(); $database = "spconfig_$guid" New-SPConfigurationDatabase -DatabaseName $database -DatabaseServer myservername\SharePoint -FarmCredentials $mycreds -Passphrase (ConvertTo-SecureString "MyVerySecurePassword" -AsPlainText -force) The PowerShell script was generating the error. The solution is simple - you need to enable IIS 6.0 Management Compat

C#: How do I call an API from a console application?

I have used the following code to call an API and pass in some parameters in the request header. Have a read  here  about the Encoding.GetEncoding(1252) . private static string ExecuteAPI(string url, Dictionary<string, string> parameters, string method = "POST", string body = " ", int timeOut = 180000) {     var request = (HttpWebRequest)WebRequest.Create(url);     request.Method = method;     foreach (var param in parameters)     {         request.Headers.Add(param.Key, param.Value);     }     if ((method == "POST") || (method == "PUT"))     {         if (!string.IsNullOrEmpty(body))         {             var requestBody = Encoding.UTF8.GetBytes(body);             request.ContentLength = requestBody.Length;             request.ContentType = "application/json";             using (var requestStream = request.GetRequestStream())             {                 requestStream.Write(requestBody, 0, requestBody.Lengt

Azure: Why is my blob 0KB when I move it?

I was archiving documents in Blob Storage and found that the target files were always being rendered as 0 KB. I was using the following code: var storageAccount = CloudStorageAccount.Parse("CONNECTIONSTRING"); var blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference("container"); var blob = blobContainer.GetBlobReference("myfilename") // Get the source as a stream Stream stream = new MemoryStream(); blob.DownloadToStream(stream); var destBlob = blobContainer.GetBlockBlobReference(blob.Name); destBlob.UploadFromStream(stream); blob.Delete(); The code should work, except for the fact that the stream needs to be repointed to the start for the download to work. blob.DownloadToStream(stream); (from above) stream.Position = 0; And now the copy/move works

Load data into a SQL Database using SqlBulkCopy, Entity Framework and reflection

I recently had the requirement to bulk load data into a SQL database. Easy enough, but since I was using the Entity Framework, most of the work has already been done for me. The plan is as follows: Use SqlBulkCopy with a DataTable as an input. In order to do this, I will need to create some headers and then add the required data as rows before blasting them into the SQL Database. ConcurrentQueue<MyObject> items = GetMyItems(); // method not included Type t = (new MyObject()).GetType(); var dt = SetHeader(t); AddItems(dt, items); Load(dt, "MyTable"); // Add the properties as columns to the datatable private DataTable SetHeader(Type t) { var dt = new DataTable(); PropertyInfo[] pi = t.GetProperties(); foreach (var p in pi) { if (string.Compare(p.PropertyType.Name, typeof(Guid).Name, true) == 0) { dt.Columns.Add(p.Name, typeof(Guid)); } else { dt.Columns.Add(p.Name); } } return dt; } // add each row as a CSV to the D

C#: How to I extend the timeout of a WebClient call? (30 seconds is just not long enough)

My Azure function is calling web services that take longer than 30 seconds to complete, so I needed to extend the default timeout the WebClient class. I created the following base class: using System; using System.Net; namespace MyHelpers {     public class MyWebClient : WebClient     {         public int Timeout { get; set; }         public WebDownload() : this( 180000 ) { }         public WebDownload(int timeout)         {             this.Timeout = timeout;         }         protected override WebRequest GetWebRequest(Uri address)         {             var request = base.GetWebRequest(address);             if (request != null)             {                 request.Timeout = this.Timeout;             }             return request;         }     } } I set the default to 3 minutes. I can then use it like I would a usual WebClient call: private string ExecuteAPI(string url, Dictionary<string, string> parameters) {     try     {         using (v

C#: How do I split a generic list into smaller chunks?

When trying to send some records to an Azure Service Bus payload, I found that due to the limits of the tenant subscription, the Service Bus could not handle the large payload I was sending. I have 256K to deal with and 1000+ records to load. A simple solution is to group the items in a logical manner, and then split them up into equal sized batches. LINQ to the rescue. Grouping the items is done as follows: var items = new List<MyObject>(); // add all the items the list var records = items.GroupBy(a => a.MyGroupingProperty); Then batch it up: var batchSize = 10; var batchedItems = records .Select((x, i) => new { Index = i, Value = x }) .GroupBy(x => x.Index / batchSize) .Select(x => x.Select(v => v.Value).ToList()) .ToList(); batchedItems is now grouped into chunks of 10. I can now iterate though the batches and handle them accordingly: foreach(var batchedItem in batchedItems) { // magic goes here. }

Entity Framework: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

The solution is simple - install the Entity Framework Nuget package to the solution startup project.

Entity Framework: No connection string named 'ImportEntities' could be found in the application config file.

I got this message when writing a PoC application for Azure. The message is quite self explanatory, but the key is WHICH PROJECT TO UPDATE. I have multiple projects, but my startup was a Console Application (for testing). The solution is to add the setting (in app.config or web.config) to the solution STARTUP PROJECT. <configuration>   <connectionStrings>     <add name="ImportEntities" connectionString="...." />   </connectionStrings> </configuration>

Azure Blob Storage: How can I quickly read a text file?

My current project has the requirement to read a large text file from blob storage and then process the contents. Getting the file is easy: var storageAccount = CloudStorageAccount.Parse(CONNECTIONSTRING); var container = storageAccount.CreateCloudBlobClient().GetContainerReference(containerName); var blob =  container.GetBlobReference(fileName); And the data can be easily read: Stream stream = new MemoryStream(); blob.DownloadToStream(stream); stream.Position = 0; string text = ""; using (StreamReader reader = new StreamReader(stream)) { text = reader.ReadToEnd(); } var lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); The real time saver came when I had to process the contents. A traditional IEnumerable was too slow, but luckily Parallel saved the day: Parallel.For(0, lines.Length - 1, i => { DoSomethingWithTheRow(lines[i]); }); Its lightening fast. A word of warning: I passed a generic into the

Azure API: A route named 'swagger_docs' is already in the route collection. Route names must be unique

Image
While creating some PoC APIs, I encountered this little gem. Fortunately, a simple fix is available: In the 'Settings' tab of the 'Publish' dialog, click 'Remove additional files at destination' (in the File Publish Options expander). You can then republish the API and the issue should be resolved.

C#: How can I create a class dynamically from text?

The following code is a really simple way to create a class dynamically from string.   dynamic data = new ExpandoObject (); IDictionary<string, object> dictionary = (IDictionary<string, object>)data; dictionary.Add("FirstName", "Richard"); dictionary.Add("Surname", "Leeman"); Console.WriteLine(data.FirstName + " " + data.Surame);

Azure Service Bus: Serialization operation failed due to unsupported type

When trying to add an object property to my Service Bus Queue, I encountered the following error: "Serialization operation failed due to unsupported type RichardLeeman.Model.MyObject." So I added the  [Serializable] annotation to the class, but to no avail. The issue seems to be that the property bag only likes simple types like int or string. So, my simple fix was to convert the class to JSON and put that in the bus. I used  System.Web.Script.Serialization.JavaScriptSerializer  to convert the object and placed the resulting string in the property bag. var client = QueueClient.CreateFromConnectionString(SERVICE_BUS_CNN, "myqueue"); var json = new JavaScriptSerializer().Serialize(myObjectInstance); var message = new BrokeredMessage("my test message"); message.Properties.Add(new KeyValuePair<string, object>("MyObjectProperty",json)); client.Send(message);

Swagger: How do I add custom return codes to my Web API?

Image
Swagger is great tool to document APIs, The standard return codes (200 - OK) do not always give enough detail about the endpoint. Fortunately, there are some simple annotations that can be added to give a better description about the return code. using Swashbuckle.Swagger.Annotations; using System.Web.Http; namespace MyNameSpace {     public class MyAPIController : ApiController     {         [HttpGet]         [Route("api/my_custom_api_route")]         [SwaggerResponse(System.Net.HttpStatusCode.OK, "All good here, thanks for asking")]         [SwaggerResponse(601, "foo message")]         public bool MyAPI()         {             return true;         }     } } The result is as follows:

Azure: How do I delete my function?

Image
I have been playing with Azure functions (which are great), but there does not seem to be an intuitive way to delete them. The first option (which requires forethought) is to create its own Resource Group (with the associated storage accounts) and delete the RG. Easy - if you knew it was coming and planned for it. The other option is go into 'Function App Settings'  -> 'Go to App Service Settings'. You can then delete the app.

Azure Function: Call a web service and return the result as a class

My current project is using Azure Functions as action triggers. Thanks to some nifty work from the Visual Studio team, we now have a local development environment. The following POC function will call a Web API (configured through API Management) and convert the resultant JSON into a class object. 1. Create a 'Model' solution that stores the class to be used. I called my MyModel. My class is called ApiResult. namespace MyModel {     public class ApiResult     {         public string Message { get; set; }     } } 2. Create an API to invoke using MyModel; using System.Web.Http; namespace MyControllers {     public class MyController : ApiController     {         [System.Web.Http.HttpGet]         [System.Web.Http.Route("api/myapiendpoint")]         public ApiResult ValidateFile()         {             return new ApiResult()             {                 Message = "Success!"             };         }     } } 3. Copy the MyMo

Azure SQL Database: Cannot connect to XXXXX.database.windows.net PART 2

My first attempt to resolve this issue (see  this  post), I was able to get past the first barrier. However, for some reason, my SSO credentials were not working. My account was configured as the system administrator account, but I could not authenticate. Then I remember that my user password was different AT THE TIME THE SQL SERVER INSTANCE WAS CREATED. I was successfully able to connect using SQL Authentication with my username and the OLD PASSWORD.

Azure SQL Database: Cannot connect to XXXXX.database.windows.net

Image
While creating an Azure POC, I create a new SQL Database and tried to connect via the online tools. I then received the following message: Fortunately, the resolution is very simple. Open the database and navigate to the Firewall Settings at the top of the Overview blade. Select 'Add client IP' to resolve the problem.

"C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props" was not found

I recently opened a sample project from a colleague and encountered the following error: "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props" was not found Fortunately, the solution was simple enough: 1. Update the Visual Studio CE 2015 Update 3 2. Install .Net Core 1.0.1 tools Preview 2 All the links are available here: https://www.microsoft.com/net/core#windowsvs2015

C#: How do I send an email from my owa site?

I recently had the requirement to send out multiple emails from my OWA account. Being a lazy person, I deceided that it would be better to automate the process instead than send it one at a time. Here is the code I generated to quickly send a test email from the account. The OWA site is https://mail.myowa.com.au/owa. using Microsoft.Exchange.WebServices.Data; using System; namespace Email {     class Program     {         static void Main(string[] args)         {             ExchangeService service = new ExchangeService();             service.Credentials = new WebCredentials("username", "password", "domain");             service.Url = new Uri(@"https://mail.myowa.com.au /EWS/Exchange.asmx ");             EmailMessage message = new EmailMessage(service);             // Add properties to the email message.             message.Subject = "Test Subject";             message.Body = new MessageBody()             {