Wednesday, September 16, 2015

SP Elevated Privilages (Impersonation)

SPUserToken systemAccountUserToken = SPContext.Current.Site.SystemAccount.UserToken;

using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.ID, systemAccountUserToken))



{
using (SPWeb cweb = elevatedSite.OpenWeb(SPContext.Current.Site.RootWeb.ID)) {

// SPSecurity.RunWithElevatedPrivileges(delegate() {

//using (SPSite csite = new SPSite(SPContext.Current.Site.ID)) {

// using (SPWeb cweb = csite.OpenWeb(SPContext.Current.Site.RootWeb.ID)) {

// SPWeb cweb = SPContext.Current.Web;





 
string strURL = cweb.Url;

//web.AllowUnsafeUpdates = true;

//Code to store in SP site

SPDocumentLibrary _MyDocLibrary = (SPDocumentLibrary)cweb.Lists["Documents"];

SPFolderCollection _MyFolders = cweb.Folders;





 
SPFolder programFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/" + "LED");



 
if (!programFolder.Exists)



{


_MyFolders.Add(strURL + "/Documents/" + "LED" + "/");



_MyDocLibrary.Update();

}
SPFolder BundlesMFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/" + "LED" + "/" + "Bundles");




if (!BundlesMFolder.Exists)



{
_MyFolders.Add(cweb.Url + "/Documents/" + "LED" + "/" + "Bundles");



_MyDocLibrary.Update();

}

 
SPFolder bundleFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber);

// SPFolder bundleFolder = cweb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber);

SPFolder siteFolder = null;




if (!bundleFolder.Exists)



{
cweb.AllowUnsafeUpdates = true;




// _MyFolders.Add(cweb.Url + "/Documents/LED/Bundles/" + bundleNumber + "/");

_MyDocLibrary.RootFolder.SubFolders.Add(cweb.Url + "/Documents/LED/Bundles/" + bundleNumber + "/");



_MyDocLibrary.Update();





}
//SPDocumentLibrary _MyDocLibrary1 = (SPDocumentLibrary)cweb.Lists["Documents"];

//SPFolderCollection _MyFolders1 = cweb.Folders;



 
if (bundleFolder.Exists)



{
siteFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber + "/" + GeolocID);

if (!siteFolder.Exists)



{
cweb.AllowUnsafeUpdates = true;

_MyDocLibrary.RootFolder.SubFolders.Add(cweb.Url + "/Documents/LED/Bundles/" + bundleNumber + "/" + GeolocID + "/");



_MyDocLibrary.Update();
// cweb.AllowUnsafeUpdates = false;



}

}


siteFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber + "/" + GeolocID);// _MyDocLibrary.RootFolder.SubFolders["DocsFolder"];

string generatedDocName = System.IO.Path.GetFileName(NewFileLoc);

Stream fileStream = File.Open(NewFileLoc, FileMode.OpenOrCreate, FileAccess.ReadWrite);



 
SPFile spfile = siteFolder.Files.Add(generatedDocName, fileStream, true);

if (siteFolder.RequiresCheckout)



{
spfile.CheckIn("Upload Comment", SPCheckinType.MajorCheckIn);

spfile.Publish("Publish Comment");



}



 

 
/////////////////////////////



}

}

Friday, February 28, 2014

Sharepoint 2013 Visio web access webpart scripts (3. Script to set active to perticular page name(Visio sheet name)

function setPage(callingNode) {
    try {
   

var vwaControl;
vwaControl= new Vwa.VwaControl(getVWAWebPartID1());
   


        // Get id from calling DOM node and set the corresponding page.
        vwaControl.setActivePage(callingNode);
    }
    catch(err) {
        alert(err);
    }
}



// Search the SharePoint page to get the WebPartID# for the Visio Web Access Web Part.
function getVWAWebPartID1() {
   
    // Get a NodesList of all the div tags on the page.
    var divArray = document.getElementsByTagName("div");
    var webPartElementID;
   
    // Iterate through the NodesList to get the node with the class attribute "VisioWebAccess."
    for (var i = 0; i < divArray.length; i++) {
        var node = divArray[i];
       
        // Return the first instance of the Visio Web Access Web Part.
        if (node.className == "VisioWebAccess") {
            webPartElementID = node.parentNode.parentNode.id;
            break;
        }
    }
    return webPartElementID;
}

Note: Now when ever we want to set particular page name as active, we will just pass the name of the page(visio page) to that script.
Ex:
onclick= "javascript:setPage('visio page sheet name')");

Monday, February 24, 2014

Sharepoint 2013 Visio web access webpart scripts (2. Script to find list of page names(Visio Sheets names)

<script language="javascript">

// Add a button to display or dismiss the custom message.
document.write("<input type='button' value='Show Message' onclick='onDiagramComplete()' />");

// Hook into the AJAX Sys.Application.load event.
Sys.Application.add_load(onApplicationLoad)

// Declare global variables.
var vwaControl;
var vwaPage;
var vwaShapes;

// Capture a reference to the current session of the Visio Web Access Web Part.
function onApplicationLoad() {
    try{
        vwaControl= new Vwa.VwaControl(getVWAWebPartID());
        vwaControl.addHandler("diagramcomplete", onDiagramComplete);
    }
    catch(err){
        alert(err);

    }
}

// Search SharePoint page to get WebPartID# for Visio Web Access Web Part.
function getVWAWebPartID() {
   
    // Get a NodesList of all the div tags on the page.
    var divArray = document.getElementsByTagName("div");
    var webPartElementID;
   
    // Iterate through the NodesList to get the node with the class attribute "VisioWebAccess."
    for (var i = 0; i < divArray.length; i++) {
        var node = divArray[i];
       
        // Return the first instance of the Visio Web Access Web Part.
        if (node.className == "VisioWebAccess") {
            webPartElementID = node.parentNode.parentNode.id;
            break;
        }
    }
    return webPartElementID;
}

// Capture references to global variables and display message in the Web Part after rendering is complete.
function onDiagramComplete() {
    try{

        // Get references to the current Visio page displayed in the Web Part and the shapes on the page.


 vwaPages = vwaControl.getAllPageIds();
        var pageCount = vwaPages.length;
        var vwaPage = vwaControl.getActivePage();
 for (var i = 0; i < pageCount; i++){
            // Get page ID from array of pages IDs.
            vwaPageId = vwaPages[i];
     
alert(vwaPageId);


//This code returns all the page names
// var vwaPage = vwaControl.getActivePage();
// var vwaPageName = vwaPage.getId();

// alert(vwaPageName);


}




    
    }
    catch(err){
        alert(err);
    }
}

</script>

Sharepoint 2013 Visio web access webpart scripts (1. Script to find active page name(Visio sheet name)




<script language="javascript">

// Add a button to display or dismiss the custom message.
document.write("<input type='button' value='Show Message' onclick='onDiagramComplete()' />");

// Hook into the AJAX Sys.Application.load event.
Sys.Application.add_load(onApplicationLoad)

// Declare global variables.
var vwaControl;
var vwaPage;
var vwaShapes;

// Capture a reference to the current session of the Visio Web Access Web Part.
function onApplicationLoad() {
    try{
        vwaControl= new Vwa.VwaControl(getVWAWebPartID());
        vwaControl.addHandler("diagramcomplete", onDiagramComplete);
    }
    catch(err){
        alert(err);

    }
}

// Search SharePoint page to get WebPartID# for Visio Web Access Web Part.
function getVWAWebPartID() {
   
    // Get a NodesList of all the div tags on the page.
    var divArray = document.getElementsByTagName("div");
    var webPartElementID;
   
    // Iterate through the NodesList to get the node with the class attribute "VisioWebAccess."
    for (var i = 0; i < divArray.length; i++) {
        var node = divArray[i];
       
        // Return the first instance of the Visio Web Access Web Part.
        if (node.className == "VisioWebAccess") {
            webPartElementID = node.parentNode.parentNode.id;
            break;
        }
    }
    return webPartElementID;
}

// Capture references to global variables and display message in the Web Part after rendering is complete.
function onDiagramComplete() {
    try{

      
        var vwaPage = vwaControl.getActivePage();
        var vwaPageName = vwaPage.getId();

alert(vwaPageName);

    
    }
    catch(err){
        alert(err);
    }
}

</script>

script to get current page URL and query string parameters





function getQueryStringParameter(paramToRetrieve) {

var currentPageUrl = "";
if (typeof this.href === "undefined") {
    currentPageUrl = document.location.toString().toLowerCase();
}
else {
    currentPageUrl = this.href.toString().toLowerCase();
}

if (currentPageUrl != null) {
        var params =
       document.URL.split("?")[1].split("&");
        var strParams = "";
        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split("=");
            if (singleParam[0] == paramToRetrieve)
                return singleParam[1];
        }
    }
    else {
        return "";
    }
  
}

var pagename=getQueryStringParameter('PageName');

Wednesday, December 4, 2013

Code to Get the SharePoint Central administration site UR


Code to Get the SharePoint Central administration site URL:

Add namespace:
using Microsoft.Sharepoint.Administration;

Add below code
 
Microsoft.SharePoint.Administration.SPAdministrationWebApplication centralWeb =
SPAdministrationWebApplication.Local;
          
string webAppUrl = centralWeb.Sites[0].Url;