Kill Database All Connections

There can be many scenarios where you need to kill active database connections. To freshen up the connection with your database, use the below SQL to kill all the connections.

ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE

 
ALTER DATABASE YourDatabase SET MULTI_USER
Advertisements

Compiling C# Code From Command Prompt

Quite often, I quickly need to test a small piece of code and if you’re doing some customization on SharePoint, I hate to open project, apply the code, publish, and deploy over the server (retract, remove and redeploy!). I post this, so I can copy and paste and run the code quickly.


using System;

class TestClass
{
    static void Main(string[] args)
    {
	Console.WriteLine("Hello World!");

       // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Run command prompt as administrator and change the directory as follows

C:\Windows\system32>cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>csc   -out:"C:\EPM\Code\my code.exe" C:\epm\code\main.cs
Microsoft (R) Visual C# Compiler version 4.6.1586.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports 
language versions up to C# 5, which is no longer the latest version. For compilers that 
support newer versions of the C# programming language, 
see http://go.microsoft.com/fwlink/?LinkID=533240

Compile the code as given below. You will have your exe file in the defined path.
Double-click the file and there you go, you’ll get your code running in no time.

The certificate chain was issued by an authority that is not trusted

Problem:

Details:
I was having the issue connecting Power BI reports from systems outside server.

“Microsoft SQL: A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 – The certificate chain was issued by an authority that is not trusted.)”

Solution:

If the port is not the default one, use database name with the port number like this

Applying CSS Styling to Read Only Project Custom Field

Had a requirement to apply CSS on a Project Custom Field, which is set to read-only through workflow stage. Challenge here was how to get div of that field where we only ‘guid’ attribute.

Following the HTML of a read-only custom field.

<div guid="cc056a66-c75c-e711-80cc-0050568d9644">

<p>​director comments</p>

</div>

After applying jquery, it picks the field as shown below and applies css.

<script src="/PWA/SiteAssets/jquery-1.10.2.min.js"><script>

$(document).ready(function(){

var myDiv = $('div[guid="cc056a66-c75c-e711-80cc-0050568d9644"]');
myDiv.attr("style", "border: 1px solid #ABABAB; padding-left:5px");

});

</script> 

After applying css, custom field HTML looks something like this;

<div style="border: 1px solid rgb(171, 171, 171); padding-left: 5px;" guid="cc056a66-c75c-e711-80cc-0050568d9644">

<p>​director comments</p>

</div>

Log Errors to Event Viewer C#

Use the following piece of code to log any error and/or information to Microsoft Windows Event Viewer. This is very useful when we need to store some information without doing extra efforts.


public static void LogError(string errorText)
        {
            string userName = string.Empty;
            if (HttpContext.Current != null)
            {
                userName = HttpContext.Current.User.Identity.Name;
                userName = userName.Remove(0, userName.IndexOf("\\") + 1);
            }

            StackTrace trace = new StackTrace();


            Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                if (!EventLog.SourceExists("MOE"))
                {
                    EventLog.CreateEventSource("MOE", "WebPart");

                }

                EventLog.WriteEntry("MOE", string.Format("User: {0} \nFile: {1} \nMethod: {2} \nLine #: {3} \nError: {4}", userName, trace.GetFrame(2).GetMethod().DeclaringType.Name, trace.GetFrame(2).GetMethod().Name, trace.GetFrame(2).GetFileLineNumber(), errorText), EventLogEntryType.Error);
            });

        }

You will have something like this;

Resetting SharePoint List ID

Use the list name and sub site name to reset SharePoint list ID. Sub site is necessary because same list name could exist in another sub site.


Declare @ListID uniqueidentifier
Declare @ListName nvarchar(500)
Declare @SubSiteName nvarchar(500)


Set @ListName='AppLog'
Set @SubSiteName='Sample Order'

Select @ListID =
--Select
 tp_ID From [dbo].[AllLists]  
Where tp_Title = @ListName --'AppLog' --'ChemicalCustomerClaim'
and tp_WebId = (Select Id from dbo.AllWebs where Title= @SubSiteName )

SELECT * FROM [dbo].[AllListsAux] 
where ListID= @ListID --'A466A9AE-C6A8-4A1A-8DBB-CA3D2122268B'


--UPDATE dbo.AllListsAux set NextAvailableId=1 
--where ListID='A466A9AE-C6A8-4A1A-8DBB-CA3D2122268B'