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
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.
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.
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.
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.
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.
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.
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.
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+
TODO:
You impersonate a user in an event receiver, and the result of calling Update() on the list item means that the "Modified By" user is that of the impersonated account, and not the original user that was logged into the site. The solution is pretty simple, just use the code below to set that user back to the original user.
SOLUTION:
private void ApplyPermissions(SPItemEventProperties properties)
{
string debugMessage = string.Empty;
Guid webId = properties.ListItem.Web.ID;
Guid siteId = properties.SiteId;
SPUserToken sysToken = null;
SPUser currentUser = null;
try
{
currentUser = properties.Web.CurrentUser;
//open site, and get elevated user
using (SPSite site = new SPSite(siteId))
{
//use our site collection
using (SPWeb myWeb = site.OpenWeb(webId))
{
sysToken = myWeb.AllUsers["i:0#.w|mydomain\\impusr"].UserToken;
}
}
bool allowUnsafeUpdates = false;
//open site with elevated user
using (SPSite site = new SPSite(properties.SiteId, sysToken))
{
//get the web
using (SPWeb myWeb = site.OpenWeb(properties.ListItem.Web.ID))
{
try
{
allowUnsafeUpdates = myWeb.AllowUnsafeUpdates; //save unsafe update setting
myWeb.AllowUnsafeUpdates = true; //allow unsafe updates for now
SPListItem elevatedListItem = null;
elevatedListItem = myWeb.Lists[properties.ListId].Items[properties.ListItem.UniqueId]; //get elevated list item
//DO ELEVATED ACTIONS HERE
base.EventFiringEnabled = false;
elevatedListItem["Editor"] = currentUser; //set current user to editor and author
elevatedListItem["Author"] = currentUser;
elevatedListItem.Update(); //update the item
base.EventFiringEnabled = true;
}
catch (Exception x)
{
throw x;
}
finally
{
myWeb.AllowUnsafeUpdates = allowUnsafeUpdates; //put back original setting
}
}//using web
}//using site
}
catch (Exception x)
{
properties.ErrorMessage = x.ToString();
}
}
NOTES:
This code ran in an ItemAdded and ItemUpdated Event Receiver