Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
chrisobriensp / ISearchResult.ts
Created August 25, 2016 16:24
A interface to represent data passed back to client.
export interface ISearchResult {
Message: string;
Results: SearchResult[];
}
@chrisobriensp
chrisobriensp / Bundle.json
Created June 25, 2016 15:48
A simple bundle.json file for a client web part in the SharePoint Framework.
{
"entries": [
{
"entry": "./lib/webparts/cobLatestnewsWp/CobLatestnewsWpWebPart.js",
"outputPath": "./dist/cob-latestnews-wp.bundle.js",
"exclude": [
"@ms/sp-client-platform"
],
"isStandalone": true,
"useWebpack": true
@chrisobriensp
chrisobriensp / CobLatestNewsWebPart.ts
Created May 21, 2016 14:56
An example showing the default boilerplate code for a new client web part (from preview tooling).
import {
IWebPartData,
IWebPartContext,
IClientSideWebPart,
BaseClientSideWebPart,
DisplayMode,
IPropertyPanePage,
IPropertyPaneFieldType
} from '@ms/sp-client-platform';
@chrisobriensp
chrisobriensp / CobRecentDocsWebPart.ts
Last active January 23, 2017 21:19
Example of a new "client web part" in the new SharePoint dev framework - shows last 5 documents you created or modified in Office 365. WARNING - THIS IS BASED ON SPFX PREVIEW; SOME CODE MAY NEED TO BE UPDATED!
import {
IWebPartData,
IWebPartContext,
IClientSideWebPart,
BaseClientSideWebPart,
DisplayMode,
IPropertyPanePage,
IPropertyPaneFieldType,
HostType
} from '@ms/sp-client-platform';
@chrisobriensp
chrisobriensp / AppForSharePoint_HomeController.cs
Created December 14, 2014 18:21
Extract from an MVC controller which authenticates to SharePoint and uses .NET CSOM code..
// code taken from an MVC controller which authenticates to SharePoint and uses .NET CSOM code. The SharePointContext and
// TokenHelper helper classes are added by Visual Studio..
[SharePointContextFilter]
public ActionResult Index()
{
User spUser = null;
// this method calls various methods on the TokenHelper class which is also added..
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
@chrisobriensp
chrisobriensp / Office365_AuthHelper.cs
Last active December 15, 2016 07:39
Authentication to Office 365 APIs using the Office 365 .NET client library
// code to authenticate to Office 365 and obtain a DiscoveryClient object, which can then be used to create
// a SharePointClient or OutlookServicesClient in order to access files/mail/calendar/contacts etc..
var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
// !!! NOTE: DO NOT USE NaiveSessionCache IN PRODUCTION. A MORE PERSISTENT CACHE SUCH AS A DATABASE IS RECOMMENDED FOR PRODUCTION USE !!!!
AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority, new NaiveSessionCache(signInUserId));
DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,
@chrisobriensp
chrisobriensp / JsomProvisioning_TaxonomyFields.js
Last active April 8, 2016 16:44
A sample showing how to use "JSOM-based provisioning" to bind taxonomy fields in SharePoint/Office 365.
'use strict';
window.COB = window.COB || {};
window.COB.TermSetName = "Countries";
window.COB.TermSetLocale = "1033";
window.COB.FieldInternalName = "COB_Countries";
window.COB.JsomProvisioning = function () {
var context,
termStore,
@chrisobriensp
chrisobriensp / GetAppOnlyClientContext.cs
Created July 10, 2014 21:07
SharePoint .NET CSOM code sample showing how to get ClientContext using "app only" authentication. (N.B. the app permissions must allow this)
// obtain web URL somehow - line below is fine for a list item RER, but you'll need another approach elsewhere..
string webUrl = properties.ItemEventProperties.WebUrl;
Uri webUri = new Uri(webUrl);
string realm = TokenHelper.GetRealmFromTargetUrl(webUri);
string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, webUri.Authority, realm).AccessToken;
using (var clientContext = TokenHelper.GetClientContextWithAccessToken(webUrl, accessToken))
{
// do CSOM stuff here..
@chrisobriensp
chrisobriensp / CSOMSearch_Full.js
Created May 1, 2014 06:22
Full CSOM code used to demonstrate executing a search, and then combining result tables (i.e. for Office 365 and SharePoint hybrid deployments) into one resultset. See accompanying blog post for notes.
var COB = COB || {};
COB.SharePoint = COB.SharePoint || {};
COB.SharePoint.HybridSearch = COB.SharePoint.HybridSearch || {};
COB.SharePoint.HybridSearch.Standard = COB.SharePoint.HybridSearch.Standard || {};
COB.SharePoint.HybridSearch.CustomInterleaving = COB.SharePoint.HybridSearch.CustomInterleaving || {};
var results;
COB.SharePoint.HybridSearch.GenericResult = (function ( ) {
var __searchResult;
@chrisobriensp
chrisobriensp / CSOMSearch_MergeResults.js
Last active August 29, 2015 14:00
Success handler code which merges results from Office 365 and on-premises sites (*if hybrid SharePoint is configured*).
function onCustomInterleavedSearchQuerySuccess() {
var mergedResults = new Array();
var fakeResultTable = {};
$.each(results.m_value.ResultTables, function (index, resultTable) {
if (resultTable.TableType === 'SpecialTermResults') {
// show best bets if we have any..
renderBestBetsResultTable(this)
}
else {