Posts

Showing posts with the label SharePoint 2010

Create SharePoint Site Collection in a specific Content DB In SharePoint using PowerShell

As the SharePoint Central Administration doesn’t provide any interface to create a site collection with a nominated content database, we need to use PowerShell commands to create a site collection in a new content database for SharePoint on-premise. #Create a SharePoint New Content Database New-SPContentDatabase -name <<WSS_Content_DB_Name>> -webapplication <<http://Web_App_URL>> Create Site Collection in the previously created Content DB #Create a Site collection in the specific content database New-SPSite -Name "<<Site_Collection_name>>" -ContentDatabase <<WSS_Content_DB_Name>> -url <<Site_Collection_URL>> -OwnerAlias "domain\primary_Admin" -SecondaryOwnerAlias "domain\Secondary_admin" -Template "BLANKINTERNET#0" Here the template is the required site collection template. Some of the common template codes are below: Team Site: STS#0 Blank Site: STS#1 Wiki Site: WIKI#0 Blog: BLOG#0 App...

How to get all the Sub-Sites under a SharePoint Site Collection (C#)?

Image
Here is the C# code for getting current site + all the sites under under current site collection. For example, here is my site structure: And the output will be as sown below: URL Title Root Site url Root Site Title Sub-site1 url Root Site Title>Sub-site1 title Sub-site1.1 url Root Site Title>Sub-site1 title>Sub-site1.1 title Sub-site2 url Root Site Title>Sub-site2 title Here is the code:                         SPWeb web = SPContext.Current.Web;                         Dictionary<string, string> AllSubsites = new Dictionary<string, string>();                         SPSecurity.RunWithElevatedPrivileges(delegate ()                         {                 ...

Create SharePoint Page and redirect to the page with edit mode

I recently found one of our clients struggle in creating a page in pages library. The process follows like this: Go to Pages library New Document Enter page name, description, select layout Click create. ->creates the page and redirects to the default view of the library. Search for the page you have just created, open the page Edit the page using the ribbon. So if you could see the struggle to create a page and reach to the edit page. So client asked me to simplify this. I found a stack-overflow post very useful and this blog post is inspired by that stack-overflow post. Solution: Put this code in a js file and refer this file in your master page. $(document).ready(function () {  var currentAddress = window.location.pathname;     if (currentAddress.toLowerCase().indexOf('/createpage.aspx') == -1) // not the creation page page         return;     $("input[id$='buttonCreatePage']").each(function () {   ...

How to open files/documents in SharePoint document library is new tab

As you know documents in SharePoint document library (images, pdf files etc) always opens in the same page and it leaves you from the SharePoint context. You can not go back to SharePoint from that page unless you press the back button of your browser. Here is a fix/workaround for this problem. Put below code to a js file and refer this file in your master page.  try {               //Below code is for opening document library files(pdf,jpg) in new tab         // has to be on an interval for grouped doc libraries         // where the actual links are loaded only once a group         // is expanded         setInterval(           function () {               $("a[onclick*='return DispEx'][target!='_blank'][aria-label$='pdf File'],a[onclick*='return DispEx'][target!='_blank'][aria-label$='jpg File']") ...

How to hide a ribbon in SharePoint 2010/2013/Online using Jquery

This post explains how to hide a SharePoint Ribbon in SP 2010/2013/ SP Online using jquery. It is very simple jquery code to hide the ribbon. For example say suppose you want to hide Add New Item Ribbon in a List called Contacts. Here is how efficiently we can achieve this. $(document).ready(function () {     var currentPage = decodeURIComponent(window.location.href.replace(/\+/g, " "));     if (currentPage.contains("Lists/Contacts")) {       window.setTimeout(hideRibbon, 500);     } }); function hideRibbon() {     $('span[id^="Ribbon.ListItem.New.NewListItem"]').hide()     window.setTimeout(hideRibbon, 200); } Above simple jquery code does the trick. As you can see, if the url is lists/contacts, we are calling the method   hideRibbon with a delay. And in the hideRibbon method we are calling the same method again with a delay. which will keep hiding the ribbon. Happy coding :)

SharePoint: Save string content as a file in Document Library from Client Side with a metadata column.

My previous post  Save string content as a file in Document Library from Client Side  explains how to add a string content as a file in SharePoint document Library. This post just extends the previous post. Suppose you have a metadata column in document library which describes some metadata about the file. For example there is a column called " FileInfo " which will describe some information about the file. So now we need to pass value to this column while adding the file. Let us see how this can be done. function AddFileToDocumentLibrary(str) {     var strm = Base64.encode(str);     strm = strm.replace(/ /g, '');     $().SPServices({         operation: "CopyIntoItems",         webURL: getCurrentUrl(),         processData: false,         async: false,         SourceUrl: "http://null",         Stream: strm, ...

The Ribbon Tab with id: "Ribbon.Read" has not been made available for this page or does not exist. Use Ribbon.MakeTabAvailable()

"The Ribbon Tab with id: "Ribbon.Read" has not been made available for this page or does not exist. Use Ribbon.MakeTabAvailable()" Have you come acrossthis error on your page? Then don't worry. its not your fault but a riddle left by MSFT.  I know that you got this error when playing with wiki pages. Yes, if wiki pages are rendered in IFrame (SPModalDialogue) or wiki page url has IsDlg=1(as part of querystring) this error shows up in sharepoint logs.  Solution: You can simply fix this by changing the type of the page from wiki to webpart page. 

SharePoint Get Current Web URL

How to get SharePoint site current URL ? We can get SharePoint Current web url in Server-Side as well as in Client side. Server Side:  As we all know current web url can be accessed in server side by web object as below. SPContext.Current.Web.Url Client Side: In client side , we can achieve this by using  SPServices  . Here is how to achieve this: Include SPServices js file in your page. Now you can access the url this way: var thisWebUrl = $().SPServices.SPGetCurrentSite(); But there is a bug in recent versions of SPServices , where the above method will return only "/". But in SharePoint 2013, we  can access web url as below as well : var thisWebUrl = _spPageContextInfo.webAbsoluteUrl; (web url) var thisSiteUrl = _spPageContextInfo.siteAbsoluteUrl; (site url) Enjoy coding :)

spFile.OpenBinary() error : file could not be opened

Are you facing this problem while opening the spfile using the url..? SPFile spFile = web.GetFile("/Document Library Name/" + FileName+ ".jpg"); byte[] binFile = spFile.OpenBinary();  <----- error that file could not be opened Here is the solution. Get the SPListItem that contains your file. Use CAML Query to get the list item by passing the column name and its value.     SPList list= web.Lists["Document Library Name"];     SPQuery query = new SPQuery();     query.Query = "<Where><Eq><FieldRef Name=\"" + columnName + "\"/><Value Type=\"Text\">" + value + "</Value></Eq></Where>";     SPListItemCollection itmColl = SPListItemCollection itmcol = list.GetItems(query);     SPFile picture=null;     if (itmcol.Count > 0)      {         picture = itmcol[0].File;      ...

Creating SharePoint State Machine Workflow

Image
State machine workflow is the powerful feature of SharePoint, which can be easily done using Visual Studio. click here to read my previous tutorial for Creating a SharePoint Sequential Visual Studio Workflow (2 or n Level)   click here to read my previous tutorial on Creating a SharePoint Sequential Visual Studio Workflow (1 Level)   Let me explain how to create a state machine workflow with a example scenario. Scenario : Let's consider a scenario in college. When a student requests for some document with the Administration office , first it goes to an officer, when he approves, it goes to higher officer for approval. When first officer approves, it has to go to next officer and when the 2nd officer approves, the workflow is complete. When first officer rejects, it has to go back to student for re-submission. When first officer approves, and second officer rejects, it has to go back to the first officer for re-approval. Let us create a state machine workflow tha...