25.1.13

Sharepoint 2013: from app to host


E ora Sharepoint 2013, accediamo al nostro sharepoint host a partire da un app su office 365

var hostweburl;
var appweburl;
 
function sharePointReady() {
    // retrieve passed app web/host web URLs..
    hostweburl = decodeURIComponent($.getUrlVar("SPHostUrl"));
    appweburl = decodeURIComponent($.getUrlVar("SPAppWebUrl"));
 
    loadDependentScripts();
}
 
function loadDependentScripts() {
    var scriptbase = hostweburl + "/_layouts/15/";
 
    // Load the js files and continue to the successHandler
    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js",
                function () { $.getScript(scriptbase + "SP.RequestExecutor.js", getAllHostWebPropsUsingREST); }
                );
        }
    );
}
 
function getAllHostWebPropsUsingREST() {
    var executor;
 
    // although we're fetching data from the host web, SP.RequestExecutor gets initialized with the app web URL..
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync(
        {
            url:
                appweburl +
                "/_api/SP.AppContextSite(@target)/web/?@target='" + hostweburl + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: onGetAllHostWebPropsUsingRESTSuccess,
            error: onGetAllHostWebPropsUsingRESTFail
        }
    );
}
 
function onGetAllHostWebPropsUsingRESTSuccess(data) {
    var jsonObject = JSON.parse(data.body);
    // note that here our jsonObject.d object (representing the web) has ALL properties because 
    // we did not select specific ones in our REST URL. However, we're just displaying the web title here..
    $('#hostWebTitle').text(jsonObject.d.Title);
}
 
function onGetAllHostWebPropsUsingRESTFail(data, errorCode, errorMessage) {
    alert('Failed to get host site. Error:' + errorMessage);
}
 
// jQuery plugin for fetching querystring parameters..
jQuery.extend({
    getUrlVars: function () {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function (name) {
        return jQuery.getUrlVars()[name];
    }
});

7.11.12

The content type is in use

using System;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      staticvoid Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("http://localhost"))
         {
            using (SPWeb webSite = siteCollection.OpenWeb())
            {
               // Get the obsolete content type.
               SPContentType obsolete = webSite.ContentTypes["Test"];

               // We have a content type.if (obsolete != null)
               {
                  IList usages = SPContentTypeUsage.GetUsages(obsolete);

                  // It is in use.if (usages.Count > 0)
                  {
                     Console.WriteLine("The content type is in use in the following locations:");
                     foreach (SPContentTypeUsage usage in usages)
                        Console.WriteLine(usage.Url);
                  }

                  // The content type is not in use.else
                  {

                     // Delete it.
                     Console.WriteLine("Deleting content type {0}...", obsolete.Name);
                     webSite.ContentTypes.Delete(obsolete.Id);
                  }
               }

               // No content type found.else
               {
                  Console.WriteLine("The content type does not exist in this site collection.");
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}