Monday, 29 September 2014

Create web application and site collection using powershell

Hi Folks,

Just came across a requirement of creating web application and site collections using power shell, posting it here.

Open an empty notepad file and copy paste below code segment :

Add-PsSnapin Microsoft.SharePoint.PowerShell

#My Parameters

$WebAppName = "MyWebApplication"
$WebAppHostHeader = "http://MyWebHost"
$WebAppPort = 7575
$WebAppAppPool = "AppPool_7575"

# This User has to be a Sharepoint Manager Account
$WebAppAppPoolAccount = "MyDomain\hirwani"

$WebAppDatabaseName = "WSS_Content_7575"
$WebAppDatabaseServer = "MySQLServer\SharePoint"
$template = Get-SPWebTemplate "STS#0"


# Create a new Sharepoint WebApplication

New-SPWebApplication -Name $WebAppName -Port $WebAppPort -HostHeader $WebAppHostHeader -URL ("http://" + $WebAppHostHeader) -ApplicationPool $WebAppAppPool -ApplicationPoolAccount ($WebAppAppPoolAccount) -DatabaseName $WebAppDatabaseName -DatabaseServer $WebAppDatabaseServer

# Create a new Sharepoint Site Collection

New-SPSite -Name "Root" -Url "http://in-mum-tfsport1:7575/" -Template $template -OwnerAlias "MyDomain\hirwani"

Save this notepad with .ps1 extension. Now execute it, you should get a new web application and site collection created.

Note : Change above parameters as per your requirement. Post here if you have any questions/suggestions.

Thursday, 4 September 2014

Adding and activating solution in solution gallery

Fea days back, i came across this requirement to add solution to site solution gallery. I wrote below code segment :

ClientContext context = new ClientContext("http://in-air-eyshr2:1234/sites/ETD02");

//Activate Publishing feature
//Add wsp from file system to SP solution gallary
Folder destionationFolder = context.Web.GetFolderByServerRelativeUrl("_catalogs/solutions");
FileCreationInformation fci = new FileCreationInformation();
fci.Content = System.IO.File.ReadAllBytes("D:/WSPS/LDTest.wsp");
fci.Url = "LDTest.wsp";
fci.Overwrite = true;
Microsoft.SharePoint.Client.File fileToUpload = destionationFolder.Files.Add(fci);
context.Load(fileToUpload);
context.ExecuteQuery();

//Activate solution
DesignPackageInfo info = new DesignPackageInfo()
{
  PackageGuid = Guid.Empty,
  MajorVersion = 1,
  MinorVersion = 1,
  PackageName = "LDTest"
  };
  Console.WriteLine("Installing design package ");
  //Relative Path of Solution Uploaded in Solution Gallery
  //string fileRelativePath = "D:/WSPS/testwsp.wsp";
string fileRelativePath = "/sites/ETD02/_catalogs/solutions/LDTest.wsp";
DesignPackage.Install(context, context.Site, info, fileRelativePath);
context.ExecuteQuery();
Console.WriteLine("Applying Design Package!");
DesignPackage.Apply(context, context.Site, info);
context.ExecuteQuery();

and it gave me following output:

You can give me your suggestions or post relevant questions, if any

Tuesday, 17 December 2013

Learning SharePoint 2013

As SharePoint 2013 hits the floor, let's start learning it as soon as possible. To start with we need to learn it's basic structure.

It's based on .Net framework 4.5. The major changes are in the development area where Microsoft has launched App model which i am going to discuss more in my coming posts. Basic look & feel will be as below:


If we go to Site Settings:



Other major enhancements are 


Continuous Crawl: There has been a major upgrade to the crawling and indexing components in SharePoint Search. Now each and every document would be available to user instantly.

Cross-site Publishing: Cross-site publishing lets you store and maintain content in one or more authoring site collections, and display this content in one or more publishing site collections. When you change the content in an authoring site collection, those changes are now displayed on all site collections that are reusing this content.This feature helps ensure content is consistent when shared across several different areas and sites. 

SkyDrive: Users can now share documents and files from a personal SkyDrive or keep them secured for their personal use. SkyDrive files can still be indexed and located through search if they have been shared to that user.

Wednesday, 25 September 2013

Add Entries in Web.Config File in SharePoint 2010

It may depend on core client requirment to modify the configuration files. I came across such requirment and below is the way to modify your configuration file. Remember one thing, before doing this activity first you take a backup of your configuration file.

First add SharePoint administration namespace using below segment:
using Microsoft.Sharepoint.Administration;

Now you can write below code segment on a button's click event.

SPWebService service = SPWebService.ContentService;

SPWebConfigModification myModification = new SPWebConfigModification();
myModification.Path = "configuration/SharePoint/SafeControls";
myModification.Name = "SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*'][@Safe='True']";
myModification.Sequence = 0;
myModification.Owner = "User Name";//In my case i used System account
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
myModification.Value = "<SafeControl Assembly='MyCustomAssembly' Namespace='MyCustomNamespace' TypeName='*' Safe='True' />";
service.WebConfigModifications.Add(myModification);

service.Update();
service.ApplyWebConfigModifications();

Monday, 20 August 2012

Content Query not visible in SharePoint 2010

Content Query is a needful webpart in Sharepoint as it relates with user very smoothely.If you are not able to see this webpart in your gallary under Content, then turn on "SharePoint Server Publishing Infrastructure" in your top level site collection administration.After this step, turn on "SharePoint Server Publishing" feature in your affected site.

NOTE: No need to go to central admin for this solution.

If you are getting any error while doing any of the above activity(as i was) then lets open PoweShell(CMD) and try below stsadm commands and will do you work:

(to activate top level site collection feature)
stsadm -o activatefeature -name publishingResources -url "SiteCollectionURL" -force

(to activate publishing feature in affected site)
stsadm -o activatefeature -name PublishingWeb -url "Affected Site URL" -force

Hope this will work.

All the best

Friday, 16 March 2012

Deploy sharepoint webpart to bin


  Simply put, I had three web parts that I wanted to package up as features into a .wsp so I could run stsadm to deploy them to different environments, such as q/a and production. I followed the following two steps:
  1. First, I used WSPBuilder to create my wsp, packaging the web parts into features. I also indicated at this point that I wanted it to be a bin deployment.
  2. Once the web part assembly dll was deployed to bin, I made the required CAS policy changes, otherwise the web parts wouldn't run. This involved a policy file and the web.config.

So, as far as step 1 goes, there are few things that will need to be done:
  1. Create an 80\bin\ folder in your solution explorer.
  2. Change your project's Build Output Path to that new folder.
  3. Change AssemblyInfo.vb to import System.Security and also to AllowPartiallyTrustedCallers.
  4. Create a mirror folder structure to the 12 hive in Solution Explorer: 12/Template/Features with a folder below that for each web part/feature to be deployed. Ensure each web part/feature folder has a feature.xml, elements.xml, and .webpart.
  5. Download WSPBuilder and install it. It will be added to the Tools menu. Click Tools -> WSPBuilder - Build WSP. The manifest.xml will be autogenerated and added to the .wsp along with the assembly's dll, and the web parts' feature.xml, elements.xml, and .webpart files.
  6. Run stsadm on the resulting .wsp to add solution, deploy solution, then activate the features.

Finally, step 2 involves setting the proper CAS permissions:
  1. Make a copy of wss_minimaltrust.config (in 12/CONFIG on server) and edit this copy as it will be the required custom policy file we need.
  2. Add a reference to SharePointPermission in the <SecurityClasses> section.
  3. Add a new custom permission set that lists the required permissions. Copy and paste the permission set SPRestricted, and in the new copy, add a line for SharePointPermission.
  4. Next, add a code group that links the newly created permission set with your assembly, so SharePoint knows when to apply the permissions.
  5. Finally, hop over to your web.config and make sure its using custom trust level, and that custom trust level is pointing to your new policy file.
If you want a step-by-step with code to copy/paste, you can find it here at How to deploy a SharePoint web part to bin. You will also find links to other pages that also show how to do this.

Wednesday, 14 March 2012

Create a SPlist and its SPView in SharePoint writing C# code against WSS object model


Create a SPlist and its SPView in SharePoint writing C# code against WSS object model

How to create a SPlist and its SPView in SharePoint writing C# code against the WSS object model ?

Assume you want to create the list and its view when activating a SharePoint Feature.
        public class myFeatureReceiver : Microsoft.SharePoint.SPFeatureReceiver
        {
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                using (SPWeb web = properties.Feature.Parent as SPWeb)
                {
                    System.Diagnostics.Debug.WriteLine("Creating the list");
                    //web.AllowUnsafeUpdates = true; //use this in an application page no need in a dll
                    web.Lists.Add("Customers""Store informations about my Company Customers",SPListTemplateType.GenericList);
                    web.Update();

                    System.Diagnostics.Debug.WriteLine("Creating the Fields");
                    SPList myNewList = web.Lists["Customers"];
                    myNewList.Fields.Add("First Name"SPFieldType.Text, false);
                    myNewList.Fields.Add("Last Name"SPFieldType.Text, false);
                    myNewList.Fields.Add("Adress"SPFieldType.Text, false);
                    myNewList.Fields.Add("City"SPFieldType.Text, false);
                    myNewList.Fields.Add("Latest Purchase Date"SPFieldType.DateTime, false);
                    myNewList.Fields.Add("Sales Comments"SPFieldType.Note, false);
                    myNewList.Update();

                    System.Diagnostics.Debug.WriteLine("Creating the view");
                    System.Collections.Specialized.StringCollection strColl = newSystem.Collections.Specialized.StringCollection();
                    strColl.Add("Title");
                    strColl.Add("First Name");
                    strColl.Add("Last Name");
                    strColl.Add("Adress");
                    strColl.Add("City");
                    strColl.Add("Latest Purchase Date");
                    strColl.Add("Sales Comments");
                    myNewList.Views.Add("Summary", strColl, @"", 100, truetrue, Microsoft.SharePoint.SPViewCollection.SPViewType.Html, false);
                    myNewList.Update();
                }
            }
        }


References

SPViewCollection.Add Method (MSDN - Microsoft.SharePoint)

Views.AddView Method (MSDN - Views Web Services) 

Pages

Followers