Category: .net

Script to browse every pages of sharepoint 2013

Add-PSSnapin Microsoft.SharePoint.PowerShell
$creds = [System.Net.CredentialCache]::DefaultCredentials
$DEBUGLOG = "c:\SharePoint.log"
$SPECIFICURLs= "c:\SharePoint.URLs.txt"

function Log([string] $msg,[string] $colour)
{
    if($colour -ne "") {
        Write-Host $msg -ForegroundColor $colour
    }
    else {
        Write-Host $msg
    }
}

function Debug([string] $msg)
{
    echo $msg >> $DEBUGLOG
}

function JoinURL([string]$a,[string]$b)
{
    if($a.EndsWith('/')) { $a=$a.Substring(0,$a.length-1) }
    if($b.StartsWith('/')) { $b=$b.Substring(1) }
    return $a + '/' + $b
}

function Fetch-WebPage([string]$url,[bool]$ignoreErrors = $false)
{
    $bypassonlocal = $false
    $proxyuri = "http://" + $env:COMPUTERNAME
    $proxy = New-Object system.Net.WebProxy($proxyuri, $bypassonlocal)

    $client = New-Object Net.WebClient
    $client.Credentials = $creds
    $client.Proxy = $proxy
    Debug "Fetching $url" 
    try {
        $pageContents = $Client.DownloadString($url) 2>&1 | Out-Null
    }
    catch {
        $msg = $error[0].ToString()
        if($ignoreErrors -eq $false) {
            Log "ERROR: $msg" "Red" 
            Debug "ERROR: $msg"
        }
    }
    $client.Dispose()
    ###Debug $pageContents
}

function ProcessWebApplication ([string]$url)
{ 
    Log "-- Root site URL: $url" "Green"
    $sitecollections = Get-SPSite $url 2>$null
    if($sitecollections -eq $null) { 
        Log "  -- No site collections found under this URL" "Cyan"
        return
    }

    $IsCA = $false
    if($sitecollections.WebApplication.IsAdministrationWebApplication) {
        Log " -- This is Central Administration" "Blue"
        $IsCA = $true
    }

    foreach($sc in $sitecollections) {
        $scUrl = JoinURL $url $sc.ServerRelativeUrl
        Log "  -- $scUrl" "Green"
        $subsites = $sc | Get-SPWeb -Limit 20
        if($subsites -eq $null) {
            Log "    -- No site collections found under this URL" "Cyan"
            continue
        }

        foreach($site in $subsites) {
            $siteUrl = JoinURL $scUrl $site.ServerRelativeUrl
            Log "    -- Fetching $siteUrl" "White"
            Fetch-WebPage $siteUrl

            # For non-Central Admin sites, try a search
            if($IsCA -eq $false) {
                # See if this site has a standard search results page
                $siteSearchUrl = JoinURL $siteUrl "/_layouts/osssearchresults.aspx?k=test"
                Log "    -- Fetching $siteSearchUrl" "White"
                Fetch-WebPage $siteSearchUrl $true

                # Bonus fetch of site search results page for Search Center sites
                $webTemplate = $site.WebTemplate
                if($webTemplate.StartsWith("SRCHCEN")) {
                    $siteSearchUrl = JoinURL $site.Url "/Pages/results.aspx?k=test"
                    Log "    -- Fetching $siteSearchUrl" "White"
                    Fetch-WebPage $siteSearchUrl
                }
            }
            $site.Dispose()
        }
    }
    Log ""
}

$zones=("Default", "Intranet", "Internet", "Custom", "Extranet")
foreach($zone in $zones) {
    Log "$zone Zone" "Yellow"
    $altURLs = Get-SPAlternateUrl -Zone $zone
    if($altURLs -eq $null) {
        Log "-- No URLs under this zone"
        continue
    }
    foreach($url in $altURLs) {
        ProcessWebApplication $url.IncomingUrl
    }
}

# Process any specific URLs
$URLFile = Get-Content $SPECIFICURLs -ErrorAction SilentlyContinue
if($URLFile -eq $null) {
    exit
}
Log ""
Log "Fetching custom URLs" "Yellow"
foreach($u in $URLFile) {
    Log "  -- Fetching $u" "White"
    Fetch-WebPage $u
}

Log "Finished."

read count : 0

This c# code can connect sharepoint 2013 through restful by username and password

Thanks God, This c# code can connect sharepoint 2013 through restful by username and password

            HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create("http://win-344c6v6tjp4:88/_api/web/lists");

            endpointRequest.Method = "GET";
            endpointRequest.Accept = "application/json;odata=verbose";
            NetworkCredential cred = new System.Net.NetworkCredential("administrator", "this is my password");
            endpointRequest.Credentials = cred;
            HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
            try
            {
                WebResponse webResponse = endpointRequest.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string response = responseReader.ReadToEnd();
                JObject jobj = JObject.Parse(response);
                JArray jarr = (JArray)jobj["d"]["results"];
                foreach (JObject j in jarr)
                {
                    Console.WriteLine(j["Title"] + " " + j["Body"]);
                }

                responseReader.Close();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.Message); Console.ReadLine();
            }

read count : 21

Powershell commands to upload/download a page to sharepoint 2013

Upload file:

Add-PSSnapIn "Microsoft.SharePoint.Powershell" 
# Set the variables 
$WebURL = “http://your_sharepoint/your_site” 
$DocLibName = "Pages"
$FilePath = “C:\a.aspx” 

# Get a variable that points to the folder 
$Web = Get-SPWeb $WebURL 

$List = $Web.GetFolder($DocLibName) 
$Files = $List.Files 

# Get just the name of the file from the whole path 
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1) 

# Load the file into a variable 
$File= Get-ChildItem $FilePath

# Upload it to SharePoint 
$Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$false) 
$web.Dispose()

Download file (Html special characters will be escaped):

Add-PSSnapin Microsoft.Sharepoint.Powershell

$WebURL = “http://kmp21dev.emsd.hksarg/kmp21dev” 
$web = Get-SPWeb $WebURL
$file = $web.GetFile('pages/homepage.aspx');
$bytes = $file.OpenBinary();

[System.IO.File]::WriteAllBytes('c:\users\spadmin\homepage.aspx', $bytes);

read count : 12

Sharepoint online bug

Sharepoint online bug – If you place an html-email-address to it and click “check” button, it crash. Even you delete all text and the check button can’t resume normal.

Sharepoint online bug
Sharepoint online bug

read count : 223

command to connect sharepoint 2013 to office web apps server

First, do these in office web apps server

new-officewebappsfarm -Verbose -InternalURL http://192.168.100.149 -ExternalURL http://192.168.100.149 -AllowHTTP -ClipartEnabled -TranslationEnabled -EditingEnabled
new-officewebappsfarm -Verbose -InternalURL http://sharepoint2013owa.petersoft.com -ExternalURL http://sharepoint2013owa.petersoft.com -AllowHTTP -ClipartEnabled -TranslationEnabled -EditingEnabled

Second, do these in sharepoint server

Remove-SPWOPIBinding -All:$true
New-SPWOPIBinding -ServerName sharepoint2013owa.petersoft.com  -AllowHTTP

read count : 302