Monday, June 18, 2012

SharePoint 2010 - Creating a Web Application By PowerShell

I found a blog, there are scripts for creating Farm, Web Application, Site Collection: http://blog.brianbeach.com (all credits to Brian). I want to write it here, like a reminder for myself. In addition, I think that the sharing knowledge is a best way to help to each other.

$WebAppURL = "http://intranet.DOMAIN.com"
$HostHeader = "intranet.DOMAIN.com"
$ContentDatabase = "Content_Intranet_Default"
$WebAppName = "Intranet Web Application"
$AppPoolName = "Intranet Content"
$AppPoolUserName = "DOMAIN\SERVICE_ACCOUNT"

Write-Host "Creating the intranet web application"
$AppPoolCred = Get-Credential $AppPoolUserName
$AppPoolAccount = New-SPManagedAccount -Credential $AppPoolCred
$AuthProvider = New-SPAuthenticationProvider
$WebApp = New-SPWebApplication -ApplicationPool $AppPoolName -ApplicationPoolAccount $AppPoolAccount -Name $WebAppName -URL $WebAppURL -HostHeader $HostHeader -Port 80 -AuthenticationProvider $AuthProvider -DatabaseName $ContentDatabase


$SiteName = "Intranet Root Site"
$OwnerEmail = "USER_NAME@DOMAIN.com"
$OwnerAlias = "DOMAIN\USER_NAME"
$SiteURL = "http://intranet.DOMAIN.com"
$SiteTemplate = "STS#1"

Write-Host "Creating a default site collection in the intranet web application"
New-SPSite -Url $SiteURL -owneralias $OwnerAlias -ownerEmail $OwnerEmail -Template $SiteTemplate
$Web = Get-SPWeb $SiteURL
$Web.title = $SiteName
$Web.update()

SharePoint 2010 - Creating the Farm By PowerShell

I found a blog, there are scripts for creating Farm, Web Application, Site Collection: http://blog.brianbeach.com (all credits to Brian). I want to write it here, like a reminder for myself. In addition, I think that the sharing knowledge is a best way to help to each other.




Add-PSSnapin Microsoft.SharePoint.Powershell -EA 0
Clear-Host

$FarmAccountName = "DOMAIN\SERVICE_ACCOUNT"
$Passphrase = "PASSWORD"
$DatabaseServer = "DATABASE_SERVER_NAME"
$ConfigDatabase = "Config"
$ContentDatabase = "Content_Admin"

Write-Host "Creating Configuration Database"
$FarmAccount = Get-Credential $FarmAccountName
$Passphrase = (ConvertTo-SecureString $Passphrase -AsPlainText -force)
New-SPConfigurationDatabase -DatabaseServer $DatabaseServer -DatabaseName $ConfigDatabase -AdministrationContentDatabaseName $ContentDatabase -Passphrase $Passphrase -FarmCredentials $FarmAccount

Write-Host "Configuring Farm"
Initialize-SPResourceSecurity
Install-SPService
Install-SPFeature -AllExistingFeatures

Write-Host "Configuring Central Administration"
New-SPCentralAdministration -Port 8080 -WindowsAuthProvider NTLM
Install-SPHelpCollection -All
Install-SPApplicationContent



How to delete the deployed Event Receiver in Sharepoint server 2010 By C#

I took it from this article




private void DeleteEventReceiverFromAList(string siteUrl)
{
using (SPSite site = new SPSite(siteUrl))
{
using(SPWeb web = site.OpenWeb())
{
try
{
SPList list = web.Lists["myList"];
if (list != null)
{
string className = "EventReceiverClass";
string asmName = "EventReceiverAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1111111111111111";
web.AllowUnsafeUpdates = true;
int receivers = list.EventReceivers.Count;
bool isAddedReceiverExist = false;
bool isUpdatedReceiverExist = false;
for (int i = 0; i < receivers; i++)
{
SPEventReceiverDefinition eventReceiver = list.EventReceivers[i];
if (eventReceiver.Class == className && eventReceiver.Type == SPEventReceiverType.ItemAdded)
{
eventReceiver.Delete();
break;
}
}
}
}
catch { }
finally
{
web.AllowUnsafeUpdates = false;
}
}
}
}

How to delete the deployed Event Receiver in Sharepoint server 2010 - PowerShell

I took it from this article

$spWeb = Get-SPWeb -Identity http://contoso.com

$spList = $spWeb.Lists["My List Name"]
$eventsCount = $spList.EventReceivers.Count
$assembly = "Project.Name.Class, Version=1.0.0.0, Culture=neutral, PublicKeyToken=11111111111"
$class = "Namespace.MyClass.ClassName"
$type = 10002
$name = "My Event Name"
for ($i = 0; $i -lt $eventsCount; $i+=1)
{
if ($spList.EventReceivers[$i].Assembly -eq
$assembly
-and
$spList.EventReceivers[$i].Class -eq $class
-and
$spList.EventReceivers[$i].Type -eq $type
-and $spList.EventReceivers[$i].Name -eq
$Name)
{
$spList.EventReceivers[$i].Delete()
}
}
$spList.Update()

Tuesday, June 12, 2012

Read SPListItemVersion in SharePoint 2010

SPListItemVersion Class return the collection of versions for a list item. If versioning is enabled on a list, each time you modify an item a version is created.

Enable SharePoint List versioning using c#:


using (SPSite spsite = new SPSite("http://contoso.com"))
{
SPWeb spweb = spsite.OpenWeb();
SPList list = spweb.Lists["ListName"];
list.EnableVersioning = true;
list.Update();
}



Retrieve SPListItem versions using c#:


public  class PageVersions
{
public string PageVersion { get; set; }
public SPUser PageVersionOwner { get; set; }
public DateTime PageVersionDate { get; set; }
}

public class GetVersionsClass
{

public PageVersions CalculateVersionPoints(SPListItem item,int prevDates)
{
PageVersions pv = new PageVersions();

SPListItemVersionCollection oVersionInfo = item.Versions;

foreach (SPListItemVersion oVersion in oVersionInfo)
{
if (oVersion.Level == SPFileLevel.Published)
{



if (oVersion.Created.ToLocalTime().CompareTo(DateTime.Now.AddDays(prevDates)) >= 0)
{
pv.PageVersion = oVersion.VersionId.ToString();
pv.PageVersionDate = oVersion.Created;
pv.PageVersionOwner = oVersion.CreatedBy.User;

}

}
}

return pv;
}
}


Monday, June 11, 2012

Custom Style and Slots in Content Query Web Part (CQWP)


The CQWP allows us to modify or create new styles which help to show custom data.
In addition by Slots we can use custom parameters and show custom fields.

For example:










1.Open CQWP in notepad or VS

2.Find "CommonViewFields"

3.Add custom columns:


Role, Text
Action, Text
Responsibility, Text




4.Open SharePoint Designer and CheckOut of:

/Style Library/XSL Style Sheets/ItemStyle.xsl

5.Insert our new template or modify old one:






















_blank








<tr style="background-color:#f7f3f7;color:#848284">
<td style="font-size:12px;color:black" valign="top">Role</td>
<td style="font-size:12px;color:black" valign="top">Action</td>
<td style="font-size:12px;color:black" valign="top">Responsibility</td>
</tr>























Thursday, June 7, 2012

New feature of .NET Framework 4.5 - Await

Most of us , who uses Silverlight Client Object Model has encountered with the problems of asynchronous programming by the Microsoft blog : Async in 4.5: Worth the Await.
I just took a piece of code from this blog, to show how it makes our lives easier :)

Synchronous example:


public static void CopyTo(Stream source, Stream destination)
{
byte[] buffer = new byte[0x1000];
int numRead;
while((numRead = source.Read(buffer, 0, buffer.Length)) > 0)
{
destination.Write(buffer, 0, numRead);
}
}


Asynchronous Programming Model:


public static IAsyncResult BeginCopyTo(Stream source, Stream destination)
{
var tcs = new TaskCompletionSource();

byte[] buffer = new byte[0x1000];
Action readWriteLoop = null;
readWriteLoop = iar =>
{
try
{
for (bool isRead = iar == null; ; isRead = !isRead)
{
switch (isRead)
{
case true:
iar = source.BeginRead(buffer, 0, buffer.Length, readResult =>
{
if (readResult.CompletedSynchronously) return;
readWriteLoop(readResult);
}, null);
if (!iar.CompletedSynchronously) return;
break;

case false:
int numRead = source.EndRead(iar);
if (numRead == 0)
{
tcs.TrySetResult(true);
return;
}
iar = destination.BeginWrite(buffer, 0, numRead, writeResult =>
{
try
{
if (writeResult.CompletedSynchronously) return;
destination.EndWrite(writeResult);
readWriteLoop(null);
}
catch(Exception e) { tcs.TrySetException(e); }
}, null);
if (!iar.CompletedSynchronously) return;
destination.EndWrite(iar);
break;
}
}
}
}
catch(Exception e) { tcs.TrySetException(e); }
};
readWriteLoop(null);

return tcs.Task;
}

public static void EndCopyTo(IAsyncResult asyncResult)
{
((Task)asyncResult).Wait();
}



New C# language async support:


public static async void CopyToAsync(Stream source, Stream destination)
{
byte[] buffer = new byte[0x1000];
int numRead;
while((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await destination.WriteAsync(buffer, 0, numRead);
}
}