Wednesday, November 21, 2018
Prevent documents from opening in the browser from SharePoint 2013
I recently had a SharePoint document library that housed Excel files. The browser would try to open the file in a tab in the browser itself. However, an error would occur with the services that allow this to happen. So I decided to tell SharePoint that when a file is clicked on, open it using the client software if it is available. To do this, go to the library in question, and click Library Settings. Choose Advanced Settings and set "Opening Documents in the Browser" to "Open in the client application".
Friday, October 26, 2018
ASP.NET Prevent double-clicking of buttons
If you have a button that causes a record to be inserted into a table, you can sometimes run into duplicate records if the user happens to click the button twice (double-click). This can be prevented by using javascript to disable the button as soon as the button is clicked the first time. Use the following code:
Then, use the OnClientClick property of the button to call this code:
NOTE: I have not used this on a form that has client validation, so I'm not sure how of if this would work in that situation.
<script>
function disableButton() {
document.getElementById("ID_OF_BUTTON").disabled = true;
}
</script>
Then, use the OnClientClick property of the button to call this code:
<asp:Button ID="ID_OF_BUTTON" runat="server" Text="Click Me" OnClientClick="disableButton();" UseSubmitBehavior="false" />
NOTE: I have not used this on a form that has client validation, so I'm not sure how of if this would work in that situation.
Thursday, September 13, 2018
Serialize .NET object to JSON
Let's say you have following classes:
And let's also say you have created an object and have assigned values as such:
To "serialize" the object as JSON use the following code. You will need to add a reference to the JSON.net library located at https://www.newtonsoft.com/json.
Public Class Output Public Property Query As QueryDetails End Class Public Class QueryDetails Public Property Count As Integer Public Property Created As DateTime Public Property Lang As String End Class
And let's also say you have created an object and have assigned values as such:
Dim MyQueryDetails As New QueryDetails With {.Count = 1, .Created = Now, .Lang = "en"} Dim MyData As New Output MyData.Query = MyQueryDetails
To "serialize" the object as JSON use the following code. You will need to add a reference to the JSON.net library located at https://www.newtonsoft.com/json.
Dim Results As String = Newtonsoft.Json.JsonConvert.SerializeObject(MyData) Console.WriteLine(Results) '{"Query":{"Count":1,"Created":"2018-09-13T10:26:13.2004198-05:00","Lang":"en"}}
Wednesday, September 12, 2018
Call a REST API from .NET
To call a REST API using the "GET" ("POST" is beyond the scope of this article) HTTP verb, use the following code:
This will populate the Results variable with the output of the web service call. Almost invariably, this will be in the JSON format. Note that all input is contained in query parameters in the API URL.
Now we need to get the JSON into a .NET object that can be easily read. This process is called deserialization. You can do this using native .NET classes, but oddly, Microsoft seems to prefer that you use the JSON.net library. This can be downloaded at https://www.newtonsoft.com/json.
Next you will need to create a series of classes that correspond to the structure of the JSON output. For example, if your output JSON looks like this:
You should create two classes like this:
Then, simply write the following code to deserialize the JSON output into the .NET Output object:
Using Client As New System.Net.WebClient Dim Results As String = Client.DownloadString("API URL") End Using
This will populate the Results variable with the output of the web service call. Almost invariably, this will be in the JSON format. Note that all input is contained in query parameters in the API URL.
Now we need to get the JSON into a .NET object that can be easily read. This process is called deserialization. You can do this using native .NET classes, but oddly, Microsoft seems to prefer that you use the JSON.net library. This can be downloaded at https://www.newtonsoft.com/json.
Next you will need to create a series of classes that correspond to the structure of the JSON output. For example, if your output JSON looks like this:
{"query":{ "count":1, "created":"2018-09-07T15:51:40Z", "lang":"en-US" } }
You should create two classes like this:
Public Class Output Public Property Query As QueryDetails End Class Public Class QueryDetails Public Property Count As Integer Public Property Created As DateTime Public Property Lang As String End Class
Then, simply write the following code to deserialize the JSON output into the .NET Output object:
Dim MyOutput As New Output Newtonsoft.Json.JsonConvert.PopulateObject(Results, MyOutput)
Thursday, August 9, 2018
Tuesday, July 24, 2018
Try Catches are not necessary for logging exceptions
I support a large system where there are try catches everywhere to log the exception. See my post Simple and secure exception handling for .NET web applications for a better way. I found an instance where instead of just removing the (unnecessary) try catch, I simply removed the line that logged the error and then threw the exception expecting the Application_Error event to log. Unfortunately, the stack trace that was logged only indicated the line number of the Throw statement and not where the error actually occurred! The lesson is, if you use a try catch, then log it right there. If not, just get rid of the try catch completely.
Thursday, July 19, 2018
Remove TFS source control from a .NET solution
Sometimes, you want to remove all TFS source control bindings from a solution. This is helpful if you are moving the solution to a different TFS project or you want to use a different source control product.
First remove the .vssscc file from the solution folder, and the .vspscc file in each individual project folder.
Then, open up the solution file in Notepad. Remove the following section completely:
First remove the .vssscc file from the solution folder, and the .vspscc file in each individual project folder.
Then, open up the solution file in Notepad. Remove the following section completely:
GlobalSection(TeamFoundationVersionControl) = preSolution SccNumberOfProjects = 2 SccEnterpriseProvider = {########-####-####-####-############} SccTeamFoundationServer = https://tfs.yourdomain.com/tfs/my_collection SccLocalPath0 = . SccProjectUniqueName1 = MyProject\\MyProject.vbproj SccProjectName1 = MySolution SccLocalPath1 = MySolution EndGlobalSection
Thursday, July 12, 2018
Add an Alpha Channel to GIMP Images to support transparency
A common task when using GIMP, is to remove the background of an image and make it transparent. Use the fuzzy select tool to select the background. An image must have something called an "alpha channel" in order to support transparency. If the image already has an alpha channel, pressing the delete button will make the background transparent. If it doesn't, pressing delete will do nothing while your frustration mounts. Simply, select Transparency in the Layer menu and choose Add Alpha Channel.
Subscribe to:
Posts (Atom)