public ScriptContext Execute(string fullPath, FileAndDate scriptEditedAndBody, ScriptContext context) { ScriptEngine engine = runtime.GetEngineByFileExtension(fullPath.Split('.').Last().ToLowerInvariant()); var paths = engine.GetSearchPaths(); try { foreach (string path in site.FeatureDefinitions[new Guid("867106ba-29b5-4301-8b78-d4597f31f22a")].Properties["Paths"] .ToString().SplitByChars(";")) { if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path)) { byte[] msg = ("Directory not found while trying to Paths from DLR feature: " + path).ToUtf8ByteArray(); engine.Runtime.IO.OutputStream.WriteAsync(msg, 0, msg.Count()); continue; } paths.Add(path); } } catch (Exception E) { byte[] exc = ("Exception while trying to load property Paths from DLR feature\n" + E.ToString()).ToUtf8ByteArray(); engine.Runtime.IO.OutputStream.WriteAsync(exc, 0, exc.Count()); } engine.SetSearchPaths(paths); byte[] importPaths = string.Format("Import paths: {0}\n", paths.JoinStrings("; ")).ToUtf8ByteArray(); engine.Runtime.IO.OutputStream.WriteAsync(importPaths, 0, importPaths.Count()); CompiledCode code = Compile(fullPath, scriptEditedAndBody, engine); context.Add("site", this.site); // TODO cache scope with preloaded libraries and namespaces ScriptScope scope = engine.CreateScope(context); code.Execute(scope); return new ScriptContext(scope.GetItems()); }
/// <summary> /// Executes a script specified by a path relative to SiteScripts, in the correct engine (DLR by default). /// Script is provided with a context that can be modified, usually with configuration and relevant SharePoint objects- /// </summary> /// <param name="scriptRelativePath"></param> /// <param name="context">Original context with variables</param> /// <returns>Resulting context with variables or, in case of error, a single item with its key starting with "_ERROR" /// and the exception as value</returns> public ScriptContext Execute(string scriptRelativePath, ScriptContext context) { string fileextension = GetFiletype(scriptRelativePath); if (fileextension == string.Empty || scriptRelativePath == fileextension) return null; FileAndDate scriptItem = Get(scriptRelativePath); var log = NaverticaLog.GetInstance(); if (scriptItem == null) return null; var filetypeMapping = AvailableExecuteScriptInterfaces(); if (filetypeMapping.Count == 0) { log.LogError(); } // find the right executor engine to execute this script IExecuteScript executorEngine; if (filetypeMapping.ContainsKey(fileextension)) executorEngine = filetypeMapping[fileextension]; else if (filetypeMapping.ContainsKey("*")) executorEngine = filetypeMapping["*"]; else throw new Exception("Script Service Client - No execution interface for files with extension " + fileextension); if (!executorEngine.Initialized) executorEngine.InitEngine(this.Site, FullPathsToAllFilesAndFolders); // run the script with the original context and return the processed context try { return executorEngine.Execute(scriptRelativePath, scriptItem, context); } catch (Exception e) { try { log.LogException(scriptRelativePath, e); } catch { throw e; } return new ScriptContext() { { "_ERROR " + scriptRelativePath, e } }; } }
/// <summary> /// Load a dllplugin from a path in SiteScripts, execute with given context /// </summary> /// <param name="fullPath"></param> /// <param name="scriptEditedAndBody"></param> /// <param name="context">Contains (at least) SPSite site</param> /// <returns></returns> public ScriptContext Execute(string fullPath, FileAndDate scriptEditedAndBody, ScriptContext context) { ILogging log = NaverticaLog.GetInstance(); Assembly dll = null; try { dll = Assembly.Load(scriptEditedAndBody.BinaryData); } catch (Exception exc) { log.LogError(fullPath, exc); } if (dll != null) { try { object instance = dll.CreateInstance(dll.DefinedTypes.First().FullName); if (instance != null) { MethodInfo getInstanceMethod = instance.GetType().GetMethod("GetInstance"); var pluginInstance = getInstanceMethod.Invoke(instance, null); MethodInfo pluginScopeMethod = instance.GetType().GetMethod("PluginScope"); var pluginScopes = pluginScopeMethod.Invoke(instance, null); if (pluginScopes != null) { foreach (PluginHost scope in ((IEnumerable) pluginScopes)) { switch (scope.GetType().Name) { case "ExecutePagePluginHost": ExecutePagePluginHost.Add(this.site, pluginInstance as IPlugin); break; case "ItemReceiverPluginHost": ItemReceiverPluginHost.Add(this.site, pluginInstance as IPlugin); break; case "ListReceiverPluginHost": ListReceiverPluginHost.Add(this.site, pluginInstance as IPlugin); break; case "TimerJobPluginHost": TimerJobPluginHost.Add(this.site, pluginInstance as IPlugin); break; } } } } } catch (ReflectionTypeLoadException rtle) { log.LogError("DLLExecute.Execute", rtle, rtle.LoaderExceptions.Select(ee => ee.ToString()).JoinStrings("\n")); } catch (Exception e) { log.LogError("DLLExecute.Execute", e); } } return context; }