Showing posts with label SharePoint. Show all posts
Showing posts with label SharePoint. Show all posts

Monday, June 12, 2017

SharePoint List versioning using c#

We can also enable SharePoint List versioning using c#

using (SPSite spsite = new SPSite("http://win7/"))
{
SPWeb spweb = spsite.OpenWeb();
SPList list = spweb.Lists["CustomList"];
list.EnableVersioning = true;
list.Update();
}

Retrieve SPListItem versions using c#

using (SPSite spsite = new SPSite("http://win7/"))
{
SPWeb spweb = spsite.OpenWeb();
SPListItemCollection listitems = spweb.GetList("http://win7/Lists/CustomList").Items;

foreach (SPListItem listitem in listitems)
{
//listitem.Versions.RestoreByID(5);
foreach (SPListItemVersion versionItem in listitem.Versions)
{
Console.WriteLine("VersionId :" + versionItem.VersionId);
Console.WriteLine("VersionLabel :" + versionItem.VersionLabel);
Console.WriteLine("IsCurrentVersion :" + versionItem.IsCurrentVersion);
Console.WriteLine("ListItem.Title :" + versionItem.ListItem.Title);
Console.WriteLine("Created :" + versionItem.Created);
Console.WriteLine("CreatedBy :" + versionItem.CreatedBy);
Console.WriteLine("Level :" + versionItem.Level);
}
}
}

Retrieve SPListItem version changes using c#

using (SPSite spsite = new SPSite("http://win7/"))
{
SPWeb spweb = spsite.OpenWeb();
SPListItemCollection listitems = spweb.GetList("http://win7/Lists/CustomList").Items;

foreach (SPListItem listitem in listitems)
{
for (int i = 0; i < listitem.Versions.Count - 1; i++)
{
SPListItemVersion oldVersion = listitem.Versions[i];
SPListItemVersion latestVersion = listitem.Versions[i + 1];
foreach (SPField field in oldVersion.Fields)
{
if (field.ShowInVersionHistory == false)
{
continue;
}

if (latestVersion == null)
{
Console.WriteLine("  > {0} changed to \"{1}\"",
field.StaticName, oldVersion[field.StaticName]);
continue;
}

if (oldVersion[field.StaticName].Equals(latestVersion[field.StaticName]))
{
continue;
}

Console.WriteLine("  > {0} changed from \"{1}\" to \"{2}\"",
field.StaticName, latestVersion[field.StaticName], oldVersion[field.StaticName]);
}
}

}
}
Console.ReadLine();


Tuesday, May 16, 2017

DateTime control "Sorry, this site hasn't been shared with you"

We developed custom web part for SharePoint 2013 using Visual Studio 2012.
In web part we used SharePoint Date Time control. For some user after clicking calendar image its showing " "Sorry, this site hasn't been shared with you". For some users its working fine, all users having same permission.
DatePickerFrameUrl="<%$SPUrl:~sitecollection/_layouts/15/iframe.aspx %>"

<SharePoint:DateTimeControl ID="dtStartdate" runat="server"
DateOnly="true" DatePickerFrameUrl="<%
$SPUrl:~sitecollection/_layouts/15/iframe.aspx %>" />

SharePoint 2013 : How to Change Site Logo Programmatically using PowerShell?

Changing the logo is a basic  when it comes to branding SharePoint 2013 sites.

If you have more site collection in SharePoint ,hundred of site collection with subsites , go with sharepoint Powershell script

Get-SPWebApplication "http://sharepoint.crescent.com" | Get-SPSite -Limit "All" | Get-SPWeb -Limit "All" |  foreach { $_.SiteLogoUrl=""; $_.update(); Write-host "Changing Logo for:"$_.Url;  }

How to change a logo across all sites/sub-sites and pages?

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue 

$SiteURL = "http://Your-Site-Collection-URL" 
#Get the site collection
$Site = Get-SPSite $SiteURL #Iterate through each site in the site collection
 foreach($web in $Site.AllWebs)
 {
 #sharepoint change logo programmatically 
$web.SiteLogoUrl = "/relative-path-logo-file" 
$web.Update() 
}

This operation can be performed only on a computer that is joined to a server farm

 You need to update the web.config to show errors rather than giving the generic errors.  I typically use “RemoteOnly” I believe…
By the sounds of it, the user you used to install SharePoint doesn’t have the correct permissions on the database.
You should have a “farm” account and an “install” account.  
I typically have an “sp_admin” account which I use for general administration and installation.  Your farm account also has particular permissions.

Thursday, April 6, 2017

Listitem.Update vs ListItem.SystemUpdate


  • SPListItem.Update() is used to update all the values in SharePoint List Item including Modified Date, Modified By, Version fields

  • SPListItem.SystemUpdate() is used to update the values of the list item without modifying the Modified Date, Modified By and Version fields.

  • SPListItem.Update() . Event Receiver is fired in case if listitemupdated,listitemupdating  event is associated. 
  • SPListItem.SystemUpdate() Event Receiver won't be fired in case if list item updated, listitemupdating  event is associated

Wednesday, April 5, 2017

Fixed: Search Results not showing Document Titles

In this case  because the managed property ‘Title’ is mapped to crawled property ‘MetadataExtractorTitle’ by default at first priority .I have recreated the below issue my machine and I have provide the solution given below.


Go to Central Administration >Application Management> Manage service application and tried to modify the order of this crawled property and move it down.
Search Schema and search for managed property ‘Title.’ Now, inside the mappings of the managed property, you should be able to see the below attachment



Move the mapped crawled property ‘MetadataExtractorTitle’ down to the last position and save the managed property




Then reset the index,perform a full crawl against the content sources and refresh any cache associated with Site Collection.
Once the full crawl is complete, you should see the correct titles for documents in search results, as follows:



Hope It will works fine. 

Friday, March 31, 2017

How to determine the SharePoint version in PowerShell

# Add the snapin, in case it's not already installed
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$ver = (Get-PSSnapin microsoft.sharepoint.powershell).Version.Major
If ($ver -eq "15" )
    {
        Write-Output "SharePoint 2013 is installed"
        }
    elseif ($ver -eq "14")
    {
        Write-Output "SharePoint 2010 is installed"
        }
        else
        {
            Write-Output "Could not determine version of SharePoint"
            } 

Monday, March 27, 2017

Get Details of all fields associated with SharePoint List

$site = new-object Microsoft.SharePoint.SPSite("http://<Site URL>") #Change site URL#
    $web = $site.openweb()
    $list = $web.Lists["<List Name>"] #Get Field Details for specified list
   
    Write-Host "List Name: " $list.Title  ##Print List title
    Write-Host "------------------------------------------------------"
    Write-Host "Field Name | Field Title "
    Write-Host "------------------------------------------------------"        
   
    foreach ($field in $list.Fields) #Get all views in lists
    {
        Write-Host $field.Title " | " $field.Type -ForegroundColor Green #Write out each field (column)                        
       
    }  
    Write-Host "------------------------------------------------------"
    Write-Host " "

    $web.Dispose()
    $site.Dispose()

SharePoint: How to get Field details from list using PowerShell ?

In this article, we retrieved the particular list properties in SharePoint Site.

#Declare the variable for below functions
$SiteCollectionURL="https://sharepointonline.sharepoint.com/sites/mysite"
$listName="Project list"


function GetSPFieldDetailsForList($SiteCollectionURL, $listName)
{
    $site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL) #Change site URL#
    $web = $site.openweb()
    $list = $web.Lists[$listName] #Get Field Details for specified list
   
    foreach ($view in $list.Views) #Get all views in lists
    {
        $spView = $web.GetViewFromUrl($view.Url) #Grab views URL
        Write-Host "List Name: " $list.Title  ##Print List title
        Write-Host "------------------------------------------------------"
        Write-Host "Field Name | Field Title "
        Write-Host "------------------------------------------------------"
        foreach ($spField in $spView.ViewFields) #Loop through all view URLs and get Fields (columns)
        {
            foreach ($field in $list.Fields) #Get all fields in lists
            {
                if($spField -eq $field.Title) #if field in lists equals field in views
                {
                    Write-Host $spField " | " $field.Type -ForegroundColor Green #Write out each field (column)                      
                }
            }
        }
        Write-Host "------------------------------------------------------"
        Write-Host " "
    }  
    $web.Dispose()
    $site.Dispose()
}

GetSPFieldDetailsForAllLists

function GetSPFieldDetailsForAllLists($SiteCollectionURL)
{
    $site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL) #Change site URL#
    $web = $site.openweb()
   
    foreach ($list in $web.Lists) #Get all list in web
    {
        foreach ($view in $list.Views) #Get all views in lists
        {
            $spView = $web.GetViewFromUrl($view.Url) #Grab views URL
            Write-Host "List Name: " $list.Title  ##Print List title
            Write-Host "------------------------------------------------------"
            Write-Host "Field Name | Field Title " -ForegroundColor DarkGreen
            Write-Host "------------------------------------------------------"
            foreach ($spField in $spView.ViewFields) #Loop through all view URLs and get Fields (columns)
            {
                foreach ($field in $list.Fields) #Get all fields in lists
                {
                    if($spField -eq $field.Title) #if field in lists equals field in views
                        {
                            Write-Host $spField " | " $field.Type -ForegroundColor Green #Write out each field (column)                        
                        }
                }
            }
            Write-Host "------------------------------------------------------"
            Write-Host " "
        }
    }
    $web.Dispose()
    $site.Dispose()
}

Friday, September 16, 2016

How to enable the officewebapps in Sharepoint 2013 using Powershell Script

Get-OfficeWebAppsFarm

Powershell Script
$owaFarm = Get-OfficeWebAppsFarm;
$owaFarm .OpenFromUrlEnabled = $true;

Thursday, June 9, 2016

SP.SOD how to use correctly?

Type Script:

<script type="text/javascript">
   LoadSodByKey("js/jquery-1.9.0.min.js", function () {
       console.log("Hello world!")
   });
</script>

Type Script
ExecuteOrDelayUntilScriptLoaded(CustomCIMOpt, "core.js");

Friday, February 12, 2016

What is the PreSaveAction

We have added a PreSaveAction JavaScript function to the page.

Return a True or False to allow the save to continue.
Example:

<script type="text/javascript">
 function PreSaveAction() {
  // pre save action alert message
  alert('form has been submitted!');
  return true;  // return true to continue with the save 
       // return false;          // or return false to cancel the save
 } 
</script>

Friday, January 29, 2016

Copy-SPSite: Rename/Recreate SharePoint site collection in SharePoint 2013

In SharePoint 2013 ,

 Copy-SPSite command :


Copy-SPSite Source Site URL [-DestinationDatabase Destination DB] -TargetUrl Target Site URL

 Remove-SPSiteUrl command:


Remove-SPSiteURL -URL Host named Site collection URL

Get all the site collection with content DB details using powershell commands

Get sites and content DB details using following power shell command

$rootSite=New-Object Microsoft.SharePoint.SPSite("http://weburl")
$spWebApp = $rootSite.WebApplication 
foreach($site in $spWebApp.Sites) {
    
    write-output "$($site.RootWeb.Url) - $($site.ContentDatabase)"     
  
    $site.Dispose() 

Wednesday, October 28, 2015

How to get login user Manager using SPServices?

var managerName;
$().SPServices({
operation: "GetUserProfileByName",
async: false,
AccountName: $().SPServices.SPGetCurrentUser(),
completefunc: function (xData, Status) {
$(xData.responseXML).find("PropertyData > Name:contains('Manager')").each(function() {
managerName = $(this).parent().find("Values").text();
alert(managerName);
});
}
});

Monday, October 19, 2015

How to list ID Custom SharePoint list Using Server Object Model?

Server Object Model:

using(SPSite osite = new SPSite(strURL))
{      
   using(SPWeb oweb = osite.OpenWeb())
   {
      SPList list = oweb.Lists["Workplan"];
string listid=list.ID.ToString();
     string listtitle=list.Title.ToString();
    }          
}

How to get with the same titles get number of List Items Count using SPServices

Script:
<script language="javascript" type="text/javascript">

  $(function() {

getcount();
});
function getcount()
{  
    var dataCount=0;
    var queryText = "<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Apple</Value></Eq></Where></Query>";
 
    $().SPServices({

        operation: "GetListItems",
        listName: "Furits",
        async: false,
        CAMLQuery: queryText,
        completefunc: function (xData, status) {          
            dataCount= $(xData.responseXML.xml).find("rs\\:data, data").attr("ItemCount");        
//In dataCount  variable ,We will get list items Count
            $("#txtcount").val(dataCount);

        }
    });
           }

</script>

HTML
<input type="text" id="txtcount"/>
 <a onclick="javascript:getcount();">Click Here</a>

Saturday, October 17, 2015

How to calculated column in SharePoint Date field?

=TEXT([Date],"mm")   =>       date is 1-Jun-2015

Result:
06
=TEXT([Date],"mmmm")  =>    date is 1-Jun-2015
Result:
January

=TEXT([Date],"mmm")   =>    date is 1-Jun-2015

Result:
Jan

=TEXT([Date],"yyyy")   =>    date is 1-Jun-2015

Result:

2015


=TEXT([Date],"dd")   =>    date is 1-Jun-2015

Result:

01