How To Download A File From A Document Library Using The SharePoint Client Object Model



TODO:

Have you ever wanted to download a document from a document library using the SharePoint client object model?

 

SOLUTION:

using SP = Microsoft.SharePoint.Client;

//class and code go here

string saveLocation = "C:\\Temp\\";

//SET YOUR ITEM HERE!!!!!!
SP.ListItem item = null;

using (SP.FileInformation fileInformation = SP.File.OpenBinaryDirect(put your context here, (string)item["FileRef"]))
{
     string fileName = (string)item["FileRef"];
     fileName = string.Concat(saveLocation, fileName.Substring(fileName.LastIndexOf("/")+1)); //get the name of the file based on last '/'

     //now save it
     using (System.IO.FileStream outPutFile = System.IO.File.OpenWrite(fileName))
     {
          fileInformation.Stream.CopyTo(outPutFile);
          fileInformation.Stream.Close();
     }
}

 

NOTES:

There are no notes on this topic

How To Get The Text Value Rather Than The HTML Value Of A MultiLine ListItem Using SharePoint Client Object Model



TODO:

You have a multi-line field in a list.  When you get the value, it comes back as HTML when you reference it as item["field"].  To get the text, rather than the HTML, you need to do the following.

 

SOLUTION:

//put at top!!!
using SP = Microsoft.SharePoint.Client;
 
//put inside class !!! 
SP.ListItem targetItem = null;
//todo, set your targetItem = to the item you want.
 
using(ClientContext clientContext = new ClientContext("http://your.url.goes.here"))
{
     var itemFieldValues = targetItem.FieldValuesAsText;
     clientContext.Load(itemFieldValues);
     clientContext.ExecuteQuery();
}
string fieldTextValue = itemFieldValues["My item Title"];

 

NOTES:

There are no notes on this topic.

Where Are The SharePoint Server 2013 Client Components SDK Dlls Installed



TODO:

You installed the SharePoint Server 2013 Client Components SDK but do not know where the Dlls are to reference in your project.


SOLUTION:

They are installed at:  C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI

 

NOTES:

There are no notes on this topic.

How To Get The Current SharePoint User That Is Logged In Using C#



TODO:

Have you ever wanted to get the current user that is logged into SharePoint?

 

SOLUTION:

//set textfield to the user
txtModificationBy.Text = this.Web.CurrentUser.LoginName;

 

NOTES:

There are no notes on this topic.

How To Fix "Could not load file or assembly '$SharePoint.Project.AssemblyFullName$" Error When Adding HttpHandler To SharePoint Application Project



 

TODO:

You add a HTTPHandler to your SharePoint project, and  you get the following error : "Parser Error Message: Could not load file or assembly '$SharePoint.Project.AssemblyFullName$' or one of its dependencies. The system cannot find the file specified."

  

SOLUTION:

The fix is simple, do the following:

1.  Open your project file in NotePad

2.  Find the PropertyGroup nodes.

3.  Add a new PropertyGroup node.

<PropertyGroup>

     <TokenReplacementFileExtensions>ashx;</TokenReplacementFileExtensions>

 </PropertyGroup>

4.  Re-Open your project file


NOTES:

There are no notes on this topic.

How to Change Application Page Title Dynamically In C# Code Behind



TODO:

Have you ever wanted to change the title of your Application Page dynamically?

 

SOLUTIONS:

protected void SetPageTitles(string Title)
{
     ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)Page.Master.FindControl("PlaceHolderPageTitle");
     contentPlaceHolder.Controls.Clear();
     LiteralControl title = new LiteralControl();
     title.Text = Title;
     contentPlaceHolder.Controls.Add(title);

     contentPlaceHolder = (ContentPlaceHolder)Page.Master.FindControl("PlaceHolderPageTitleInTitleArea");
     contentPlaceHolder.Controls.Clear();
     title = new LiteralControl();
     title.Text = Title;
     contentPlaceHolder.Controls.Add(title);
}

 

NOTES:

There are no notes on this topic.

How To Fix "Internet Information Services Is Not Installed" Error When Running SharePoint 2010 Products Configuration Wizard



TODO:

You run the SharePoint Products Configuration Wizard, and you receive the error "Internet Information Services Is Not Installed", even though you have IIS installed.  the issue is the IIS 6 features are not installed, and are required.

 

SOLUTION:

Install IIS6 features, and your issue will go away.

 

NOTES:

There are no notes on this topic.

How To Change The Virtual Directory Name From _Layouts To A Custom Value For Sharepoint Application Pages



TODO:

Have you ever wanted to change the location of your Application Pages from _Layouts to a directory that is more meaningful to your application?

 

SOLUTIONS:

1.  Open IIS Manager and go to your Sharepoint site.

2.  Create a new virtual directory at the same level as _layouts  (for example 'MyDirectoryName')

3.  Point the new virtual directory to the same physical path as your _layouts virtual directory. (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\template\layouts)

4.  Now open your application page, changing _layouts to MyDirectoryName.

5.  Now you have a more meaningful URL to use for your application pages.

 

NOTES:

What is nice about this solution, is that you do not need to do anything special during deployment.

How To Secure Sharepoint Application Pages By User Group



TODO:

Have you ever wanted to secure an Sharepoint Application Page by User Group?  If so, just use the method below, and call it in Page_Load.

 

SOLUTION:

/// <summary>
/// Validate that the page can be accessed
/// </summary>
/// <returns></returns>
protected void ValidatePageAccess(string[] AllowedUserGroups)
{
     try
     {
          //get web object
          using (SPWeb currentWeb = this.Web)
          {                    
               #region Allowed Groups

               SPGroup allowedGroup = null;
               bool userHasAccess = false;

               //check each, if one group is OK, all are.
               foreach (string group in AllowedUserGroups)
               {
                   allowedGroup = currentWeb.Groups[group];

                   //see if user is in allowed group
                   if (allowedGroup.ContainsCurrentUser)
                   {
                       if (Request.HttpMethod == "POST")
                       {
                           SPUtility.ValidateFormDigest();
                       }
                       userHasAccess = true;
                       break;
                   }
               }
               
               //now check access
               if(!userHasAccess)
               {
                    throw new Exception("User does not have access");
               }
               #endregion 
          }
     }
     catch (Exception x)
     {
          Response.Redirect("/_layouts/accessdenied.aspx");
     }
}

 

NOTES:

There are no notes on this topic.

Page_Load Is Called Twice



 TODO:

You have an Application Page, or regular ASPX page, .Net 3.5 and you notice that each Postback is happening twice, for no apparent reason.

 

SOLUTION:

If you have an IMG SRC="" tag, or a placeholder like IMG SRC="#" this will cause the issue.  If dynamically generating SRC values, be sure that it is actually being populated with a valid image path.

 

NOTES:

This bug has been fixed in .Net 4.0+