/// <summary>Generate the drop box edit details.</summary> /// <param name="file">The file to edit.</param> /// <param name="web">The web the file is in.</param> /// <param name="mode">The open mode.</param> /// <param name="sourceUrl">The source page.</param> /// <returns></returns> public DropBoxEditDetails GenerateDropBoxEditDetails(AssignmentFile file, SPWeb web, DropBoxEditMode mode, string sourceUrl) { DropBoxEditDetails details = new DropBoxEditDetails(); // Set default details details.Url = file.Url; details.OnClick = EditJavascript(file, web); try { bool isIpad = SlkUtilities.IsIpad(); if (settings.UseOfficeWebApps) { if (file.IsOfficeFile) { if (settings.OpenOfficeInIpadApp && isIpad) { details.Url = file.GenerateOfficeProtocolUrl(web, sourceUrl); details.OnClick = null; } else if (mode == DropBoxEditMode.Edit) { // Document must be 2010 format to be edited in office web apps if (file.IsOwaCompatible) { details.Url = file.GenerateOfficeAppsEditUrl(web, sourceUrl); details.OnClick = null; } } // Use Office Web Apps viewer for office files except for Excel which does not support it for pre 2010 worksheets else if (file.Extension.ToUpperInvariant() != "XLS") { details.Url = file.GenerateOfficeAppsViewUrl(web, sourceUrl); details.OnClick = null; } } } else if (settings.OpenOfficeInIpadApp && isIpad) { details.Url = file.GenerateOfficeProtocolUrl(web, sourceUrl); details.OnClick = null; } } catch (ArgumentOutOfRangeException) { // Not valid for office web apps details.Url = file.Url; details.OnClick = EditJavascript(file, web); } return details; }
/// <summary>Copies the original file to the student's drop box.</summary> /// <returns>The url of the file.</returns> public AssignmentFile CopyFileToDropBox() { AssignmentFile assignmentFile = null; SPSecurity.RunWithElevatedPrivileges(delegate() { SharePointFileLocation fileLocation; if (!SharePointFileLocation.TryParse(assignmentProperties.Location, out fileLocation)) { throw new SafeToDisplayException(SlkFrameset.FRM_DocumentNotFound); } using (SPSite sourceSite = new SPSite(fileLocation.SiteId)) { using (SPWeb sourceWeb = sourceSite.OpenWeb(fileLocation.WebId)) { SPFile file = sourceWeb.GetFile(fileLocation.FileId); if (file.Exists == false) { string message = string.Format(CultureInfo.CurrentUICulture, culture.Resources.AssignmentFileDoesNotExist, fileLocation.FileId, assignmentProperties.Title); store.LogError(message); throw new SafeToDisplayException(message); } if (MustCopyFileToDropBox(file.Name)) { try { assignmentFile = SaveFile(file); } catch (SPException) { // Retry in case a temporary error try { assignmentFile = SaveFile(file); } catch (SPException e) { string message = string.Format(CultureInfo.CurrentUICulture, culture.Resources.DropBoxFailedToCopyFile, file.Name, assignmentProperties.Title, e.Message); store.LogError(message); string safeMessage = string.Format(CultureInfo.CurrentUICulture, culture.Resources.DropBoxFailedToCopyFile, file.Name, assignmentProperties.Title, string.Empty); throw new SafeToDisplayException(safeMessage); } } } } } }); return assignmentFile; }
/// <summary>Generates the edit javascript script.</summary> /// <param name="file">The file to generate the js for.</param> /// <param name="web">The web the file is in.</param> /// <returns>The script.</returns> static string EditJavascript(AssignmentFile file, SPWeb web) { if (file.IsEditable) { #if SP2007 string script = "editDocumentWithProgID2('{0}', '', 'SharePoint.OpenDocuments','0','{1}','0');"; return string.Format(CultureInfo.InvariantCulture, script, Microsoft.SharePoint.Utilities.SPHttpUtility.UrlPathEncode(file.Url, false), web.Url); #else return CreateDispExFunctionCall(file, web); #endif } else { return string.Empty; } }
private AssignmentFile SaveFile(SPFile file) { AssignmentFile assignmentFile = null; using (SPSite destinationSite = new SPSite(assignmentProperties.SPSiteGuid)) { destinationSite.CatchAccessDeniedException = false; using (SPWeb destinationWeb = destinationSite.OpenWeb(assignmentProperties.SPWebGuid)) { using (new AllowUnsafeUpdates(destinationWeb)) { // Temporarily turn off property promotion. Possible cause for exceptions saving docs. I don't actually know // if settings this will disable it as without an update it's not persisted and we don't want to persist the // setting, just turn it off for this addition. It may even work asynchronously. destinationWeb.ParserEnabled = false; SPUser learner = CurrentUser; DropBox dropBox = new DropBox(store, destinationWeb); AssignmentFolder assignmentFolder = dropBox.GetAssignmentFolder(assignmentProperties); AssignmentFolder learnerSubFolder = null; if (assignmentFolder == null) { assignmentFolder = dropBox.CreateAssignmentFolder(assignmentProperties); } // ApplyAssignmentPermission creates the learner folder if required ApplyAssignmentPermission(learner, SPRoleType.Contributor, SPRoleType.Reader, true); learnerSubFolder = assignmentFolder.FindLearnerFolder(learner); using (Stream stream = file.OpenBinaryStream()) { assignmentFile = learnerSubFolder.SaveFile(file.Name, stream, new SlkUser(learner)); } } } } return assignmentFile; }
static string CreateDispExFunctionCall(AssignmentFile file, SPWeb web) { string text = Microsoft.SharePoint.Utilities.SPUtility.MapToControl(web, file.Name, string.Empty); // string openInBrowser = (file.Item.ParentList.DefaultItemOpen == DefaultItemOpen.Browser) ? "1" : "0"; string openInBrowser = "0"; // Always 0 as want to open in client application // string forceCheckout = file.Item.ParentList.ForceCheckout ? "1" : "0"; string forceCheckout = "0"; // Drop box doesn't support check in/out. string currentUser = (web.CurrentUser != null) ? web.CurrentUser.ID.ToString(CultureInfo.InvariantCulture) : string.Empty; // string isCheckedOutToLocal = (string)file.Item["IsCheckedoutToLocal"], string isCheckedOutToLocal = "0"; // Drop box doesn't support check in/out. string permMask = file.PermMask; /* SPFieldLookupValue sPFieldLookupValue = file.Item["CheckedOutUserId"] as SPFieldLookupValue; string scriptLiteralToEncode = (sPFieldLookupValue == null) ? string.Empty : sPFieldLookupValue.LookupValue; scriptLiteralToEncode = SPHttpUtility.EcmaScriptStringLiteralEncode(scriptLiteralToEncode); */ string scriptLiteralToEncode = string.Empty; // Drop box doesn't support check in/out. return string.Format(CultureInfo.InvariantCulture, "DispEx(this,event,'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}');", new object[] { "TRUE", "FALSE", "TRUE", text, openInBrowser, text, string.Empty, string.Empty, scriptLiteralToEncode, currentUser, forceCheckout, isCheckedOutToLocal, permMask }); }
static bool MustCopyFileToDropBox(string fileName) { return AssignmentFile.MustCopyFileToDropBox(Path.GetExtension(fileName)); }