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.

Advertisements

Deleting List Items Through PowerShell Filtered Using CAML Query

Use the following to delete items older than the specified date in CAML below and limiting to 500 all together.

$site = new-object Microsoft.SharePoint.SPSite("<a href="http://dhldepmweb01/PWA">http://dhldepmweb01/PWA</a>")

$web = $site.rootweb

$list = $web.Lists["Project Server Workflow Tasks"]

$caml='<Where>

<Leq>

<FieldRef Name="Created" />

<Value IncludeTimeValue="TRUE" Type="DateTime">2015-12-31T02:32:20Z</Value>

</Leq>

</Where>'

$query=new-object Microsoft.SharePoint.SPQuery

$query.Query=$caml | Write-Output

$items=$list.GetItems($query)

$listItemsTotal = $items.Count;

$count=0

#Limiting the deletion till 500 count

for($x=$listItemsTotal-1;$x -ge 0 -And $count -le 500; $x--)

{

$items[$x].Delete()

$count++

}

$web.Dispose()

$site.Dispose()

Adding Resource Calendar Exceptions Programmatically

ResourceDataSet rds = _resourceClient.ReadResource(new Guid(resGuid));

// be sure that the resource is checked out !

if (rds.CalendarExceptions != null)
{
for (int i = 0; i &lt; rds.CalendarExceptions.Rows.Count; i++)
{
// check row data...

// delete row if necessary
rds.CalendarExceptions.Rows[i].Delete();
break;
}
// don´t use GetChanges() method on the dataset !
_resourceClient.UpdateResources(resourceDataSet, false, false);
}

// Read resource data again
rds = _resourceClient.ReadResource(new Guid(resGuid));

// create new datarow
ResourceDataSet.CalendarExceptionsRow exceptionsRow = rds.CalendarExceptions.NewCalendarExceptionsRow();

// set necessary data for the new exception row here
//exceptionsRow.Start
//exceptionsRow.Finish
//exceptionsRow.Name
//exceptionsRow.RES_UID
//...

// update the resource again
_resourceClient.UpdateResources(resourceDataSet, false, false);

...
// don´t forget to check in the resource