So pretty much everyone knows how to create a new site collection, but what is not so simple is creating a new site collection in a content database which you specified. This can easily be achieved using some powershell, as shown below:
$SITECOLLECTION = Read-Host "What is the name of the site collection you wish to add?"
$SITEDESCRIPTION = Read-Host "What is the site collection description?"
$SITEURL = Read-Host "What is the URL of the site you wish to add?"
$SITEADMINISTRATOR = Read-Host "Who is the site collection administrator?"
$DATABASENAME = Read-Host "In which content database do you wish to add this site collection?"
$SITETEMPLATE = "CMSPUBLISHING#0" # --> THIS IS SITE TEMPLATE FOR PUBLISHING SITE
# CHECK IF CONTENT DATABASE EXIST
$CONTENTDB = Get-SPContentDatabase -identity $DATABASENAME
# IF CONTENT DB EXISTS WE WILL ADD SITE COLLECTION AND USE CONTENT DB SPECIFIED ELSE DISPLAY ERROR MSG
If (!$CONTENTDB)
{
Write-Host "The specified content database has not been found - PLEASE TRY AGAIN"
}
else
{
Write-Host "SITE COLLECTION CREATION STARTED"
New-SPSite -Name $SITECOLLECTION -Url $SITEURL -Template $SITETEMPLATE -OwnerAlias $SITEADMINISTRATOR -ContentDatabase $CONTENTDB -Description $SITEDESCRIPTION
Write-Host "SITE COLLECTION CREATION COMPLETE"
}
I hope this helps you as it did me.
Over the years, I’ve performed many technical roles — from hands-on engineering to enterprise architecture — and always found joy in sharing ideas through blogging. After a bit of a hiatus from blogging, I thought I’d start again and try my luck at putting thoughts out there. My hope is to spark collaboration, challenge assumptions, and maybe even help us do things a little better. So here are my ramblings for now…
Thursday, May 9, 2013
Wednesday, May 8, 2013
How to retrieve s user profile in SharePoint 2010
The below code will show how to retrieve a user profile using the SharePoint object model
The following references are required:
{
try
{
using (var site = new SPSite(siteCollectionUrl))
{
var serverContext = SPServiceContext.GetContext(site);
var profileManager = new UserProfileManager(serverContext);
var userProfile = profileManager.GetUserProfile(currentUser.RawSid);
return userProfile != null ? userProfile : null;
}
}
catch (Exception ex)
{
// Handle error and write them to a log
return null;
}
}
I hope this has helped you, please feel free to drop me a note if you have any questions.
The following references are required:
- Microsoft.Office.Server
- Microsoft.Office.Server.UseProfiles
- Microsoft.SharePoint
- System.Web
{
try
{
using (var site = new SPSite(siteCollectionUrl))
{
var serverContext = SPServiceContext.GetContext(site);
var profileManager = new UserProfileManager(serverContext);
var userProfile = profileManager.GetUserProfile(currentUser.RawSid);
return userProfile != null ? userProfile : null;
}
}
catch (Exception ex)
{
// Handle error and write them to a log
return null;
}
}
I hope this has helped you, please feel free to drop me a note if you have any questions.
Default user profile properties
If you ever need to figure out what are the default user profiles properties provided by SharePoint 2010 - you can follow this link.
You may also want to have a look at the Microsoft.Office.Server.UserProfiles.PropertyConstants class, you may find it here.
I hope this has helped you as it did me.
You may also want to have a look at the Microsoft.Office.Server.UserProfiles.PropertyConstants class, you may find it here.
I hope this has helped you as it did me.
Saturday, July 14, 2012
How to loop through all SPWeb object in your SharePoint site using Powershell
If ever you need to loop through all your SPWeb object in a SharePoint site for whatever reason and you want to do it in powershell. Try out the below code snippet. It worked for me, hope it can help you.
Happy scripting :-)
Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinueAnd that's it, you can now loop through all SPWeb objects in your site.
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$siteUrl = read-host "What is your site url? Eg: http://mysharepointpotal"
$site= Get-SPSite $siteURL
$webs= $site.AllWebs
try
{
foreach ($web in $webs)
{
Write-Host $web.url
# Perform operation
}
}
catch
{
write-host -f Red "error" $_.exception
$errorlabel = $true
}
Happy scripting :-)
Tuesday, July 3, 2012
Programatically creating a SharePoint publishing page
Creating a new SharePoint publishing page is pretty simple, we just got a couple of things to remember.
First check if the page already exists, if so you can edit that page. If it does not exist we need to pass a filename and a page layout which are pretty important.
Once the page is created you can do pretty much anything with it.
private static PublishingPage CreatePage(string filename, string title, string siteUrl, string pageLayout)
{
try
{
using (SPSite s = new SPSite(siteUrl))
{
using (SPWeb w = s.OpenWeb())
{
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(w);
PageLayout[] ls = pWeb.GetAvailablePageLayouts();
var layout = ls.ToList().Find(x => x.Title.Equals(pageLayout, StringComparison.InvariantCultureIgnoreCase));
if (layout == null)
return null;
var p = pWeb.GetPublishingPages().ToList().Find(x => x.Name.Equals(filename, StringComparison.InvariantCultureIgnoreCase));
if (p == null)
p = pWeb.GetPublishingPages().Add(filename, layout);
else
return p;
p.Title = title;
p.Update();
return p;
}
}
}
catch
{
return null;
}
}
And there you have it you have successfuly created a new SharePoint publishing page.
First check if the page already exists, if so you can edit that page. If it does not exist we need to pass a filename and a page layout which are pretty important.
Once the page is created you can do pretty much anything with it.
private static PublishingPage CreatePage(string filename, string title, string siteUrl, string pageLayout)
{
try
{
using (SPSite s = new SPSite(siteUrl))
{
using (SPWeb w = s.OpenWeb())
{
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(w);
PageLayout[] ls = pWeb.GetAvailablePageLayouts();
var layout = ls.ToList().Find(x => x.Title.Equals(pageLayout, StringComparison.InvariantCultureIgnoreCase));
if (layout == null)
return null;
var p = pWeb.GetPublishingPages().ToList().Find(x => x.Name.Equals(filename, StringComparison.InvariantCultureIgnoreCase));
if (p == null)
p = pWeb.GetPublishingPages().Add(filename, layout);
else
return p;
p.Title = title;
p.Update();
return p;
}
}
}
catch
{
return null;
}
}
And there you have it you have successfuly created a new SharePoint publishing page.
Monday, June 11, 2012
How to serialize and deserialize any object to a compressed string
How to : Serialize and Deserialize a object into a compressed string
If you have ever needed to serialize some sort of object to a string for whatever reason we always trying to google some example of how to do it correctly. In the example below, I am going to serialize and compress a generic list to a string.
Serialization and Compression
Deserialize object
Happy coding guys...
If you have ever needed to serialize some sort of object to a string for whatever reason we always trying to google some example of how to do it correctly. In the example below, I am going to serialize and compress a generic list to a string.
Serialization and Compression
private static string CompressValue(object serializedValue)
{
try
{
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, serializedValue);
byte[] inbyt = ms.ToArray();
using (MemoryStream objStream = new MemoryStream())
{
using (DeflateStream objZS = new DeflateStream(objStream, CompressionMode.Compress))
{
objZS.Write(inbyt, 0, inbyt.Length);
objZS.Flush();
objZS.Close();
byte[] b = objStream.ToArray();
return Convert.ToBase64String(b);
}
}
}
}
catch { return string.Empty; }
}
Deserialize object
private ListAnd that is it, you can now serialized any object into a string and deserialize into your object again.
Happy coding guys...
ASP.NET Update Panel not reloading jquery
Issue: asp.net update panel not reloading custom jquery after a partial post back occurs.
Cause: The asp.net update panel reloads all the content within the content template tags, thus your custom jquery is not reloaded and never fired.
Solution: Add your custom jquery at the end of the request by using the page request manager object:
And problem solved, it's as easy as that.
I hope this helped you, it solved my problem.
Cause: The asp.net update panel reloads all the content within the content template tags, thus your custom jquery is not reloaded and never fired.
Solution: Add your custom jquery at the end of the request by using the page request manager object:
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(function () {
DOSTUFF();
});
And problem solved, it's as easy as that.
I hope this helped you, it solved my problem.
Subscribe to:
Posts (Atom)